jia340
jia340

Reputation: 11

Threads are not alive using python multithreading?

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

Answers (1)

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 the run() method starts until just after the run() method terminates. The module function enumerate() returns a list of all alive threads.

Upvotes: 3

Related Questions