1

I have a simple python app that will not terminate if i use queue.join(). Below is the code:

import threading
import Queue

q = Queue.Queue()

for i in range(5):
    q.put("BLAH")

def worker():
    while True:
         print q.qsize()
         a = q.get()
         print q.qsize()
         q.task_done()
         print q.qsize()


for i in range(2):
    t = threading.Thread(target=worker())
    t.daemon = True
    t.start()

q.join()

I've also created a watchdog thread that print's threading.enumerate(), then sleeps for 2 seconds. The only thread left is the MainThread, and the queue size is in fact 0. This script will never terminate. I have to ctrl + z, then kill it. What's going on?

1
  • 1
    change thread creation to t = threading.Thread(target=worker) Commented Apr 19, 2013 at 6:25

2 Answers 2

1
t = threading.Thread(target=worker)

You want to pass a reference to the worker function, you should not call it.

Sign up to request clarification or add additional context in comments.

Comments

0

worker function does not exit, therefore it will not join. Second you probably want to join thread not queue.

I'm not an expert in python threading, but queue is just for data passing between threads.

1 Comment

Python's Queue does in fact allow the join operation, which indicates that all queued items have been popped with a corresponding call to task_done. Further, because a daemon thread is used, the main application will be allowed to exit even though the daemon thread is still running.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.