1

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!

0

1 Answer 1

3

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.

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

Comments

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.