Tumgik
#queue.queue
Text
Another attempt at some sashiko! This one is in the same thigh-rubbing spot as previous, but on a different set of jean shorts. Given how much I have to wrangle patches in this particular spot, I'm genuinely tempted to just put one in right as I buy the pants, maybe with some cute fabric so it's gradually revealed?
Tumblr media
I'm about halfway done here, and you can tell just how much drawing out the grid (in this case, with lines every quarter-inch) helped me get the pattern I was aiming for much more effectively than the last one, where I was freehanding the lines.
Tumblr media
Originally, the plan with this one was to have all the zig-zags point the same way, but honestly? I kinda dig the tiered waterfall vibe on this one more! Quite the improvement, in comparison to the one previous, I'd say, and that all comes down to technique.
I really enjoy the process of sashiko mends, it's very satisfying to load up a few straight stitches onto the needle just so where they need to be, then smooth out the fabrics they pin together, over and over. Very meditative, which makes it a good way to keep the hands busy while listening to a podcast! (Like, say, my podcast, Paper Cuts, perhaps? :3) plus, after you're done, you get to return a piece of clothing to use you would have lost otherwise! Isn't that a delight on its own?
38 notes · View notes
codehunter · 1 year
Text
Queue.Queue vs. collections.deque
I need a queue which multiple threads can put stuff into, and multiple threads may read from.
Python has at least two queue classes, Queue.Queue and collections.deque, with the former seemingly using the latter internally. Both claim to be thread-safe in the documentation.
However, the Queue docs also state:
collections.deque is an alternativeimplementation of unbounded queueswith fast atomic append() andpopleft() operations that do notrequire locking.
Which I guess I don't quite unterstand: Does this mean deque isn't fully thread-safe after all?
If it is, I may not fully understand the difference between the two classes. I can see that Queue adds blocking functionality. On the other hand, it loses some deque features like support for the in-operator.
Accessing the internal deque object directly, is
x in Queue().deque
thread-safe?
Also, why does Queue employ a mutex for it's operations when deque is thread-safe already?
https://codehunter.cc/a/python/queue-queue-vs-collections-deque
0 notes
Text
python pygame image load and button that sends a message to a threaded queue
import pygame import threading import queue # Set up the message queue message_queue = queue.Queue() def process_queue(): while True: message = message_queue.get() print("Processing message:", message) # Start the queue processing thread queue_thread = threading.Thread(target=process_queue, daemon=True) queue_thread.start() pygame.init() # Set up the display screen = pygame.display.set_mode((400, 300)) pygame.display.set_caption("Pygame GUI Example") # Define some colors black = (0, 0, 0) white = (255, 255, 255) blue = (0, 0, 255) # Load an image image = pygame.image.load("a.png") # Define a button button_font = pygame.font.Font(None, 20) button_text = button_font.render("Click me!", True, black) button_rect = button_text.get_rect() button_rect.center = (200, 200) # Run the game loop running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False elif event.type == pygame.MOUSEBUTTONUP: mouse_pos = pygame.mouse.get_pos() if button_rect.collidepoint(mouse_pos): message_queue.put("Button was clicked!") # Clear the screen screen.fill(white) # Draw the image on the screen screen.blit(image, (0, 0)) # Draw the button on the screen screen.blit(button_text, button_rect) # Update the display pygame.display.update() # Quit pygame pygame.quit()
0 notes
56perc · 4 years
Text
컨디션을 통한 스레드 동기화 예제
컨디션을 통한 스레드 동기화 예제
동시성을 다룰 때에는 특정한 자원을 동시에 액세스하지 못하도록 관리하거나 여러 작업들이 시작되는 시점을 맞추는 동기화 수단이 필요할 수 있다. Lock은 특정 코드 영역을 동시에 여러 스레드가 실행하지 못하도록 보호할 때 사용하며, 이벤트는 여러 스레드들이 특정 이벤트가 발생할 때까지 기다리다가 동시에 시작될 수 있도록 한다. 컨디션(Condition)은 락과 이벤트가 결합되어 있는 동기화 수단이다.
컨디션은 락을 내재하고 있는 이벤트라 할 수 있다. 락과 마찬가지로 acquire() ~ release() 구간이 있어 한 번에 하나의 스레드/프로세스가 실행되는 영역을 만들 수 있는데, 그 사이에 wait()를 통해서 이벤트를 기다릴 수 있다. 이때 한 스레드가 락을 잠근 상태에서 wait()를…
View On WordPress
0 notes
blubberquark · 5 years
Text
AsyncIO for the working PyGame programmer (part IV: Using Threading Instead)
In this post I will show you how to do the same thing again, but with threads, for the sake of comparison. The first example had no networking, the second sent HTTP requests from the game loop with the requests module, and the third used asyncio and aiohttp.
If you want to take a look at the previous examples, look here!
This version uses requests and the threading module in Python 3. It should not produce any lag in the game loop. Apart from the HUD, the gameplay experience is the same. Both threading and asyncio are fine choices if you want to add background i/o to your game.
This time we'll walk through the code piece by piece.
import pygame import requests import threading import queue import getpass
We import requests, threading and queue this time. The same requests module has already been used in the networking example without concurrency. The nice thing about threading is that we can just do normal blocking i/o, but in another thread, without changing the code too much.
The queue module contains a data structure we use to communicate between threads. It is important that you don't just use a list, and that you use the right queue data structure for multi-threaded code. There is a different queue data structure for async code.
pygame.init() blob_yposition=30 blob_yspeed=0 achievement=False gravity=1 screen_size=640,480 screen=pygame.display.set_mode(screen_size) clock=pygame.time.Clock() running=True
This bit of the code is the same as in the other versions.
posting_queue=queue.Queue() def achievement_posting_loop(): while running or posting_queue.qsize(): payload=posting_queue.get() if payload is None: posting_queue.task_done() break try: for i in range(10): response = requests.post("https://httpbin.org/post", data=payload) if response.status_code == requests.codes.ok: print("achievement posted") print(response.content) else: print("something went wrong") except requests.exceptions.ConnectionError: print("something went wrong") posting_queue.task_done() print("Thread Ending") background_thread=threading.Thread(target=achievement_posting_loop) background_thread.start()
Here we create a queue, define a function, and start a thread that runs the function.
In the thread, we poll the queue for new items as long as the running variable is true, or as long as there are still items in the queue. This way, we cans set running to false, and the posting loop will first work through the queue before exiting. On the other hand, if the queue is empty, but running is true, we wait until there is another item out into the queue. By calling task_done(), we can communicate to the other thread that the queue item has been processed. This enables the other thread to call join() on the queue, to wait until the queue is emptied and all items taken from the queue have been processed.
flying_frames=0 best=0 color=(50,50,50) font=pygame.font.SysFont("Helvetica Neue,Helvetica,Ubuntu Sans,Bitstream Vera Sans,DejaVu Sans,Latin Modern Sans,Liberation Sans,Nimbus Sans L,Noto Sans,Calibri,Futura,Beteckna,Arial", 16) while running: clock.tick(30) events=pygame.event.get() for e in events: if e.type==pygame.QUIT: running=False if e.type==pygame.KEYDOWN and e.key==pygame.K_UP: blob_yspeed+=10 # ... # move sprites around, collision detection, etc blob_yposition+=blob_yspeed blob_yspeed-=gravity if blob_yposition<=30: blob_yspeed=0 blob_yposition=30 flying_frames=0 else: flying_frames+=1 if flying_frames>best: best=flying_frames if not achievement and best>300: # 10 seconds payload=dict(name=getpass.getuser(), title="ten seconds", subtitle="last for ten seconds without touching the ground") posting_queue.put(payload) achievement=True color=(100,0,0)
As you can see, there is not much to do here apart from putting an item into the queue.
if blob_yposition>480: blob_yposition=480 blob_yspeed=-1*abs(blob_yspeed) # ... # draw screen.fill((255,255,255)) pygame.draw.rect(screen,color, pygame.Rect(screen_size[0]/2, screen_size[1]-blob_yposition, 18,25)) fps=clock.get_fps() q_size=posting_queue.qsize() message=f"current:{flying_frames//30}, best:{best//30}, fps:{fps:.2f}, queue:{q_size}" surf=font.render(message, True, (0,0,0)) screen.blit(surf,(0,0)) pygame.display.update() posting_queue.put(None) posting_queue.join() background_thread.join() print("Thank you for playing!")
Finally, when the player quits the game, we signal the background thread to stop, and wait for the queue elements to be processed. The achievement_posting_loop() function will return on receiving None in the queue, so we don't have to "kill" the background thread, bit we can wait for it to finish with background_thread.join().
And that's it. This is the threading based version of the game.
Extra Credit: If you want to do something more "useful" than posting to HTTPbin in these code examples, take a look at Discord web hooks: https://birdie0.github.io/discord-webhooks-guide/
This snippet should help you get started:
username=getpass.getuser() payload=dict( username="Achievement Example Code Bot", content=f"{username} stayed up in the air for ten seconds without touching the ground" ) response = requests.post(MY_DISCORD_WEBHOOK_URL, json=payload)
In part five, we will leave this example game behind, and go back to asyncio, to implement a "real-world" example of asynchronous game networking: Twitch chat integration!
2 notes · View notes
mlbors · 7 years
Text
Using Queues
Through this small article, we are going to see what Queues are. Let's get into it!
A Queue is a data structure that is really similar to a real-world queue. A Queue is open at both ends and one end is used to insert data while the other is used to remove data.
A Queue keeps elements in first-in first-out order (FIFO), so the first element we inserted is the first one to come out.
In a way, Queues are similar to Stacks and can be implemented using Array, Structure, Pointer or Linked List.
When we add an item to the Queue, we enqueue and when we remove an item from the Queue, we dequeue. We can also peek to get the element at the front of the Queue without removing it.
Let’s make a quick and small implementation using Java where we will use Array.
public class TheQueue { private String[] queueArray; private int queueSize; private int front, rear, numberOfItems = 0; TheQueue(int size) { queueSize = size; queueArray = new String[size]; front = 0; rear = -1; numberOfItems = 0; } // Enqueue operation public void enqueue(String input) { if(numberOfItems + 1 <= queueSize) { rear++; queueArray[rear] = input; numberOfItems++; System.out.println("ENQUEUE: " + input + " was added to the Queue."); } else { System.out.println("The Queue is full."); } } // Dequeue operation public void dequeue() { if(numberOfItems > 0) { System.out.println("DEQUEUE: " + queueArray[front] + " was removed from the Queue."); queueArray[front] = "-1"; front++; numberOfItems--; } else { System.out.println("The Queue is empty"); } } // Peek front operation public void peekFront() { System.out.println("PEEK FRONT: " + queueArray[front]); } // Peek rear operation public void peekRear() { System.out.println("PEEK REAR: " + queueArray[rear]); } // Using our Queue public static void main(String[] args){ TheQueue theQueue = new TheQueue(10); theQueue.enqueue("45"); theQueue.enqueue("100"); theQueue.enqueue("98"); theQueue.enqueue("25"); theQueue.enqueue("11"); theQueue.peekFront(); theQueue.peekRear(); theQueue.dequeue(); theQueue.dequeue(); theQueue.peekFront(); theQueue.peekRear(); } }
We can also do something similar using Python:
class Queue: def __init__(self, size): self.queue = [] self.size = size # Enqueue operation def enqueue(self, item): if len(self.queue) < self.size: self.queue.append(item) return self.queue else: return "The Queue is full." # Deqeueue operation def dequeue(self): if len(self.queue) > 0: return self.queue.pop(0) else: return "The Queue is empty." # Using our Queue if __name__ == '__main__': queue = Queue(10) queue.enqueue("45") queue.enqueue("100") queue.enqueue("98") queue.enqueue("25") queue.enqueue("11") print(queue.queue) queue.dequeue() queue.dequeue() print(queue.queue)
0 notes
Text
Tumblr media
These two visible mends are a bit different from the others, in that I did them (well, 99% of them) on stream! We spent a good volume of time working on that triforce, which was made out of a woven picot stitch, using some dyed-yellow sashiko thread. Near the end of the stream, however, we noticed that the corner of the other pocket also needed some repair, and added a few black stitches to make a little void patch.
Tumblr media
After the stream, I decided the little guy needed eyes, so I threw two silver french knots on the top of the affair. Personally, it reminds me of the grue, from Zork, just two eyes flashing in the dark, but I've gotten several people mentioning the soot sprites, which, admittedly? I can see!
https://youtu.be/gLOhWEJrpeo?si=fjAfOlNdICsHMM4X want to hang out while I work on this? Like I said, I did it on stream, here's the VOD!
21 notes · View notes
Text
Time for another traditional embroidery mend! This time, we're trying out the Puncetto Valsesiano stitch, which, after I finished this up, I've found, is an entire style of needlelace! For this patch, though, we just did stitch after stitch after stitch, no fancy patterning. (I might do fancy patterning later, to be fair! been reading up on things, and I've got a few smaller repairs to make that would suit it quite nicely!)
Tumblr media
One row of stitches in, and I was thinking, oh this can't be so bad! this is actually quite soothing, all these repetitive actions, and with such a delightful, knitting-like texture!
Tumblr media
This many stitches in, and sure, I'm still having a good time, but mostly, I'm finding out how much thread this takes! Poor razz, @razzmatazic, I thought this was going to be a simple, small patch, so I borrowed her thread to work on this one! You can't quite tell in this shot, but I'm actually about to run out of thread!
Tumblr media
After likely much, much more fuss than was strictly necessary, including accidentally picking a slightly different color of floss because of some mislabeled strands, noticing I'd dropped enough stitches to need to throw some extra ceylon stitching in a gap, and a probably pretty noticable shift in texture because of single vs. double threading, we've got ourselves a patch that, even despite its shortcomings, I really dig the look of!
Tumblr media Tumblr media
Unfortunately, I didn't make this patch wide enough generally, so I've got a few more holes that'll be getting filled by a big 'ol sashiko patch that's going to layer overtop the puncetto, eventually! Stay tuned, I'm just as curious as you are as to how that'll look.
20 notes · View notes
Text
I haven't been posting about this here, but I've been doing a good deal of visible mending, figured now is a good time to start posting!
Up first, we've got a little sashiko! It's no secret these comfy elastic-fibered jeans have trouble holding up to anything beyond basic wear, so I threw a (loosely adapted) yarai pattern on these jeans to get them holding together longer!
Tumblr media Tumblr media
This being my first pass at sashiko, it's far from my best work (I get better later, as you'll see soonventually) but I still find it quite charming, if I'm honest! These jeans would have been toast if I hadn't put this patch on, and the sashiko technique here reinforces a weak spot quite nicely, so my thighs don't absolutely wreck these jeans quite so quickly.
13 notes · View notes
Text
Okay, so brace yourselves gang, this one's a bit long as it's actually secretly 3 mends! First up, we've got some simple sashiko crosses with some nice contrasting bee-themed yellow fabric! You might not be able to tell by the way I've got that grid laid out partially in the progress shot there, but originally the idea was to have this one follow a pattern I'd found from someone on youtube that had a sort of 3 dimensional look to it, but I miscounted some stitches, and so had to stick with crosses instead.
Tumblr media
As we can see here, it's pretty blatantly cleaner with the grid to use as reference! sure, my stitch length isn't exactly the same every time, but to be honest with you? I could probably only nail that with the stick and stitch patterns, they're at least roughly correct!
Tumblr media
Unfortunately, I also mis-placed the fabric slightly in the process of that patch, so that leads me to the other two mends here! Coming off decorating that mask, I had a good bit of remaining embroidery thread in bi colors. Found a tutorial for the ceylon stitch, liked the way it came together, and since it's really great at squares, decided I could use another little bi flag, much closer to properly proportioned this time!
Tumblr media Tumblr media Tumblr media Tumblr media
Last but not least here, we've got a flag for demisexuality, which, honestly? a piece of my identity I don't bring up much! I figured, though, in for a penny with the pride mends, in for a pound, and honestly, the little chevron wasn't even all that hard, I just adapted a crochet pattern for the stitch count. (Really, the hard bit was the purple stripe in the middle, as I had to chain all those single stitches together with only the black triangle as an anchor!)
Tumblr media Tumblr media Tumblr media
I absolutely love the way this particular mend came together, even if it was mostly a recovery from fumbles. I swear, these jorts are cursed, they only got this sashiko patch because the normal denim patch I put on them previously wasn't the right size, either! Plus, after I wore them a bit more, the other side of the seam wore out, too, so I've still gotta apply a patch there, but hey, it's more wears than I would have gotten out of them normally anyway!
Tumblr media
17 notes · View notes
Text
My tshirts generally have a real troublesome habit of developing holes where the seams meet in the armpit, so here's one take on solving that, so I can keep my shirts hanging around for as long as possible!
Tumblr media
Stitch of choice here is the ceylon stitch! I was originally using some of the leftover blue thread from the bi flag mend, but as we can see in the next picture, that turned out to be pretty scant and not quite enough for the whole mend.
Tumblr media
Not only did I buy some non-sunfaded blue embroidery thread to continue here, but also, someone gifted me the cutest little needle minders, they're cats in teacups! The contrast between the two threads isn't super visible here where there's only one row, but you can really tell in the finished patch that the first thread I bought had seen no small volume of sun before getting used. (Suppose that's what I get for buying thread from a visibly sunfaded display in that joanns!)
Tumblr media
Even despite how this turned out, with the two-toned blue thing going on, and the little bit in the top left corner where I dropped stitches and went back to fill them in after the fact, I still actually really dig how it looks. Something something the passage of time, imperfections let your soul out of it, y'know?
Over time, as this piece has settled in, I actually really enjoy how it sits on this shirt. Initially, the stitching made one giant thick mat that I could feel being large and annoying in my armpit, but a few runs through the laundry later and it's much, much more comfortable. If it weren't for the additional wear this puts on already high-stress areas, I'd honestly love to keep doing this to cover holes too small to sashiko patch, but too big for other techniques.
(Future me from the perspective of this mend finds out that satin stitches do just fine, and there's plenty of decorative patterns to put those in!)
14 notes · View notes
Text
Breaking up the trend of visible mends here, I just had an idea for something nice and went for it! So, originally, I was out west seeing Razz while target had their pride collection out, and we spotted a purple crop top with this super fresh bi flag finger guns design on it. Lamenting ensued, because I'm not anywhere NEAR confident enough in showing off my gut to be wearing that.
However! I did prevail! I got a few blank white fabric masks from my then-parent company when the plague came down in earnest the first time, and I'd been keeping them around in case I run out of decorative fabric masks to wear over the good 95% filter ones. Anyway, time passes, I'm up in Boston by the time of this tale, and Pride rolls around once more. I go "hm. Should wear something fun to pride, what if I embroidered one of those masks?" Rustled up a picture of the design, and set to it!
Tumblr media
Step 1: hey, these little pink thumbs are pretty easy, I'll have this done in no time! The stitches are pretty even and straight, too, I'm doing great for never embroidering before!
Tumblr media
Step 2: "oh jeez these ones took approximately forever and I had to redo the outline as I went and AAAA the stitching has gaps in it!" I say, barely getting the purple section done with few hours to spare before pride tomorrow.
Tumblr media
Step 3: "there's no WAY I'm gonna finish the blue stitching in enough time to not be tired for pride tomorrow, quick, switch to satin stitch!", thinks Nester! I really did almost run out of time here, just barely got to sleep in time to catch my alarm the next day to head to pride! Glad the few little black lines to define the fingers were really simple and quick stitches!
Tumblr media
Just for fun, here's the back, which is MUCH cleaner than my projects usually are.
Overall I really dig how this one came out, and it made such a fun statement piece to wear to pride!
18 notes · View notes
Text
python threaded non-blocking input
import sys import threading import time import queue def add_input(input_queue): while True: input_queue.put(sys.stdin.read(1)) def do_input(the_buf_str): clean_input = the_buf_str.strip('\r').strip('\n') print(clean_input, flush=True) pass def nonblocking_input_manager(): interval_freq = 0.5 # seconds input_queue = queue.Queue() input_thread = threading.Thread(target=add_input, args=(input_queue,)) input_thread.daemon = True input_thread.start() last_update = time.time() str_command_buf = '' # when user presses enter we store the buffer here while True: time.sleep(0.01) # slow down that cpu a bit if time.time() - last_update > interval_freq: sys.stdout.write(".") sys.stdout.flush() last_update = time.time() if not input_queue.empty(): buf = input_queue.get() for ch in buf: str_command_buf += ch else: # show buffer if str_command_buf != '': do_input(str_command_buf) str_command_buf = '' nonblocking_input_manager()
0 notes
Text
python threaded queue manager with subclass for further customization
import time import threading import queue # main class class c_thread_queue_manager(threading.Thread): q_speed = .001 # class global for loop cycle delay def __init__(self, queue): threading.Thread.__init__(self) self.queue = queue self.daemon = True self.start() self.q_speed = .001 # delay between cycles def process_data(self,the_data): # override print("data:[",the_data,"]", flush=True) time.sleep(3) pass def loop_process_begin(self): # override pass def loop_process_end(self): # override pass def run(self): self.t_running = True # set to false when we wanna bail while self.t_running: self.loop_process_begin() ### SPEED OF QUEUE PROCESSING time.sleep(self.q_speed) if self.queue.empty(): pass # nothing in the queue to do else: data = self.queue.get() if data == "#exit#": self.t_running = False else: ### DATA IN QUEUE. PROCESS self.process_data(data) pass self.loop_process_end() ### all done run # sub class (split processing away from loop manager) class c_server_heartbeat(c_thread_queue_manager): # shuffles incoming lines to thread queues def __init__(self, queue): c_thread_queue_manager.__init__(self,queue) self.tick_speed = 2 self.processing = False def loop_process_begin(self): # overridden pass def loop_process_end(self): # overridden time.sleep(self.tick_speed) #print("tick", flush=True) pass def process_data(self,the_data): # overridden print(the_data) pass # example created from subclass: # (step1: create queue, step 2: create thread and pass queue to it) q_hb_server = queue.Queue() # make queue hb_server = c_server_heartbeat(q_hb_server) # make server # now add something to the queue for processing... q_hb_server.put("hello world")
0 notes
Text
Pinned post time!
Tumblr lets you pin posts so I’m pinning my tag list and list of links you can find me other places because my stuff deserves EYES, gosh dang it!
Links first so you maybe can click them!
Twitter (@Glacier_Nester): https://twitter.com/Glacier_Nester 
Twitch (Glacier_Nester): https://www.twitch.tv/glacier_nester
(Live Friday nights for reading books aloud, and Tuesday nights for games, both tabletop and video!)
Youtube (Glacier Nester): www.YouTube.com/user/glaciernester  
(Where all the VODs go after I’m done streaming on twitch, and also other little stray video nonsense I put together!)
Discord server (For the igloo, my fan community): discord.gg/PBZNsjn 
My podcast, Paper Cuts, which is edited down versions of the Friday night books: papercutsbooks.libsyn.com
Last (But not least), my AO3: https://archiveofourown.org/users/Glacier_Nester/
Now, tags!
Queue.Queue (Queued posts)
Texty Text (text post)
Static (audio post)
A Thousand Words (picture post)
Thousands of Words (video post)
Someone’s Speaking (quote post)
Linky Link (post has links)
Chaotic Conversations (chat post)
Rebagel (a reblogged post)
Kids in a garage (code lyoko)
Glacier’s blitherblather (me talking)
Word Vomit (my writing)
Aspen Forests, Opportune Castles, Razzberry Tart (Posts for specific people)
I’ve got my quarters (Ready player 1)
Doctor whyforhowcome (doctor who)
capture those flags (ingress)
gimme dat (giveaway posts)
send me mail (Ask games)
*wheezes* (I laughed)
I’m cry (sad post)
pitchforks (Activism whatnot)
spacebug (firefly)
ohgodopinions (opinions on things! oh no!)
too many warehouses (warehouse 13)
surelocked (sherlock)
stuck at home (in the trash)
warty hogs (harry potter, damn shame about what Rowling did, hm?)
puntastic (puns I wanna find again)
monsters in my pocket (pokemon)
the gamiest grumps you ever will see (game grumps)
asawow academeme (asagao)
boxing club is best club (asagao fight club)
in like flynn (tron)
Back in Time (back to the future)
glowing arby’s (night vale)
rushing to post (rush)
terrible phd (dr. horrible)
Blue corn chips (percy jackson)
schween:exit (dick:out meme)
glacier’s mailbag (I answered an ask)
legand of zeldo (zelda)
Macademia nut cookies (my hero academia)
beepleboople (synthwave/cyberpunk vibes)
best final fantasy (bravely default/second)
gay space rocks (steven universe)
dungeons AND dragons? that’s far too much!
Mai is bi and so am I (Mai asagaoacademy)
Red hood who? (teen titans)
sproutleboople (solarpunk)
potoo pavilion (owl house)
marvelling at detectives (comics, general)
young master old padawan (Star wars)
VHS deck full of eyes (magnus archives)
Everybody Votes Website (polls!)
Nesterian lifestylings (non content stuff I'm doing and decided to do a 4-H style writeup on for fun)
Also a general note: I started backtagging old posts, but I’m nowhere near done with that, so some of these posts that come up in these tags may be VERY OLD! Check the post dates, please!
1 note · View note
codehunter · 2 years
Text
Flask: Background thread sees a non-empty queue as empty
When I run a Flask app in uwsgi, the background thread and the app functions see different values when querying size of the same Queue.
Components
A Flask application with a thread-safe queue.
A GET call returns the queue size.
A POST call adds an element to the Queue.
A background thread prints the Queue size
The problem
When the app is from the shell using python tester.py, I get the expected result:
2014-06-07 14:20:50.677995 Queue size is: 0127.0.0.1 - - [07/Jun/2014 14:20:51] "POST /addMessage/X HTTP/1.1" 200 -2014-06-07 14:20:51.679277 Queue size is: 12014-06-07 14:20:52.680425 Queue size is: 12014-06-07 14:20:53.681566 Queue size is: 12014-06-07 14:20:54.682708 Queue size is: 1127.0.0.1 - - [07/Jun/2014 14:20:55] "POST /addMessage/Y HTTP/1.1" 200 -2014-06-07 14:20:55.687755 Queue size is: 22014-06-07 14:20:56.688867 Queue size is: 2
However, when the app is executed using uwsgi, I get the following in the logs:
2014-06-07 14:17:42.056863 Queue size is: 02014-06-07 14:17:43.057952 Queue size is: 0[pid: 9879|app: 0|req: 6/6] 127.0.0.1 () {24 vars in 280 bytes} [Sat Jun 7 14:17:43 2014] POST /addMessage/X => generated 16 bytes in 0 msecs (HTTP/1.1 200) 2 headers in 71 bytes (1 switches on core 0)2014-06-07 14:17:44.059037 Queue size is: 02014-06-07 14:17:45.060118 Queue size is: 0[pid: 9879|app: 0|req: 7/7] 127.0.0.1 () {24 vars in 280 bytes} [Sat Jun 7 14:17:45 2014] POST /addMessage/X => generated 16 bytes in 0 msecs (HTTP/1.1 200) 2 headers in 71 bytes (1 switches on core 0)2014-06-07 14:17:46.061205 Queue size is: 02014-06-07 14:17:47.062286 Queue size is: 0
When running under uwsgi, the background thread does not see the same queue as the app. Why is that? How can I make these two threads look at the same Queue object?
Updates
I see inconsistent behaviour even when it's executed as a Python script: Sometimes it does not manage to log messages (using app.logger), and I can only see prints. This means that the thread is running, but it can't do anything with app.logger.
uwsgi .ini configuration
[uwsgi]http-socket = :9002plugin = pythonwsgi-file = /home/ubuntu/threadtest-uwsgi.pyenable-threads = trueworkers = 1chdir = /home/ubuntu/thread-tester/thread_tester
Code
from flask import Flask, jsonifyimport Queuefrom threading import Threadimport timeimport datetimeimport loggingimport syslogging.basicConfig(stream=sys.stderr, format='%(asctime)s %(levelname)s - %(message)s')app = Flask(__name__)messages = Queue.Queue()def print_queue_size(): while True: app.logger.debug("%s Queue size is: %d" % (datetime.datetime.now(), messages.qsize())) time.sleep(1)t = Thread(target=print_queue_size, args=())t.setDaemon(True)t.start()@app.route("/queueSize", methods=["GET"])def get_queue_size(): return jsonify({"qsize": messages.qsize()}), [email protected]("/addMessage/<message>", methods=["POST"])def add_message_to_queue(message): messages.put(message) return jsonify({"qsize": messages.qsize()}), 200if __name__ == "__main__": app.run(port=6000)
https://codehunter.cc/a/flask/flask-background-thread-sees-a-non-empty-queue-as-empty
0 notes