python threading blocks

I am trying to write a program which creates new threads in a loop, and doesn’t wait for them to finish.

As I understand it if I use .start() on the thread, my main loop should just continue, and the other thread will go off and do its work at the same time

However once my new thread starts, the loop blocks until the thread completes.

Have I misunderstood how threading works in Python, or is there something stupid I’m doing?

Here is my code for creating new threads.

def MainLoop():
    print 'started'
    while 1:
        if not workQ.empty():
            newThread = threading.Thread(target=DoWorkItem(), args=())
            newThread.daemon = True
            print 'queue empty'

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

This calls the function and passes its result as target:

threading.Thread(target=DoWorkItem(), args=())

Lose the parentheses to pass the function object itself:

threading.Thread(target=DoWorkItem, args=())

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply