Note that there are some explanatory texts on larger screens.

plurals
  1. POPython Queue waiting for thread before getting next item
    text
    copied!<p>I have a queue that always needs to be ready to process items when they are added to it. The function that runs on each item in the queue creates and starts thread to execute the operation in the background so the program can go do other things. </p> <p>However, the function I am calling on each item in the queue simply starts the thread and then completes execution, regardless of whether or not the thread it started completed. Because of this, the loop will move on to the next item in the queue before the program is done processing the last item.</p> <p>Here is code to better demonstrate what I am trying to do:</p> <pre><code>queue = Queue.Queue() t = threading.Thread(target=worker) t.start() def addTask(): queue.put(SomeObject()) def worker(): while True: try: # If an item is put onto the queue, immediately execute it (unless # an item on the queue is still being processed, in which case wait # for it to complete before moving on to the next item in the queue) item = queue.get() runTests(item) # I want to wait for 'runTests' to complete before moving past this point except Queue.Empty, err: # If the queue is empty, just keep running the loop until something # is put on top of it. pass def runTests(args): op_thread = SomeThread(args) op_thread.start() # My problem is once this last line 't.start()' starts the thread, # the 'runTests' function completes operation, but the operation executed # by some thread is not yet done executing because it is still running in # the background. I do not want the 'runTests' function to actually complete # execution until the operation in thread t is done executing. """t.join()""" # I tried putting this line after 't.start()', but that did not solve anything. # I have commented it out because it is not necessary to demonstrate what # I am trying to do, but I just wanted to show that I tried it. </code></pre> <p>Some notes:</p> <p>This is all running in a PyGTK application. Once the 'SomeThread' operation is complete, it sends a callback to the GUI to display the results of the operation.</p> <p>I do not know how much this affects the issue I am having, but I thought it might be important.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload