AdamL
AdamL

Reputation: 51

Python threading app not terminating

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?

Upvotes: 1

Views: 634

Answers (2)

aisbaa
aisbaa

Reputation: 10633

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.

Upvotes: 0

FogleBird
FogleBird

Reputation: 76752

t = threading.Thread(target=worker)

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

Upvotes: 1

Related Questions