Reputation: 11
I'm using the multithreading module of python but the code doesn't work sometimes. I print the count of active threads and found that sometimes the started threads are not active. Here is the code:
def do_stuff(q,obj):
while not q.empty():
item=q.get()
print item
q.task_done()
for i in range(num_thread):
worker=threading.Thread(target=do_stuff,args=(q,Files,))
worker.setDaemon(True)
worker.start()
print worker.is_alive()
print threading.active_count()
print threading.enumerate()
I got False for the is_alive() function and there's only one thread (the main thread) in the active thread list.
What am I doing wrong here?
Thanks a lot!
Upvotes: 1
Views: 1400
Reputation: 133909
Your threads did not have time to start yet.
As per documentation
is_alive()
isAlive()
Return whether the thread is alive.
This method returns
True
just before therun()
method starts until just after therun()
method terminates. The module functionenumerate()
returns a list of all alive threads.
Upvotes: 3