Jason Donnald
Jason Donnald

Reputation: 2316

how to wait for dynamically started multiple threads in python to finish

I am using threading in python to spawn multiple threads dynamically. This is how I am spawning the threads:

def func(max_val):

    for val in range(0,max_val):
        thread1 = threading.Thread(target=func9, args=(val,))
        thread1.start()

    print 'Ended all threads' #this should get printed once all the threads have ended
    # bunch of other code after this
    .
    .
    .


if __name__ == '__main__':
    ret = func()

What I want that once all the threads have been spawned then process should wait till all the threads have ended and then proceed with next line in code. I know that we use thread1.join() to wait for a thread but if I place this inside the for loop then it will wait for 1st thread to end before spawning the next thread. I don't want it to wait for one thread to end before starting the next thread. It should spawn all the threads simultaneously and then wait for all of them to end before executing next line in code (like the print in func() above should be executed once all the threads have ended).

How can I do this?

Upvotes: 1

Views: 1695

Answers (2)

Iron Fist
Iron Fist

Reputation: 10951

Referring to Python Docs:

join() raises a RuntimeError if an attempt is made to join the current thread as that would cause a deadlock. It is also an error to join() a thread before it has been started and attempts to do so raises the same exception.

You can do it this way without even saving them to a list (Example taken from here):

main_thread = threading.currentThread()
for t in threading.enumerate():
    if t is main_thread:
        continue
    t.join()

enumerate() returns a list of active Thread instances

Upvotes: 2

Sait
Sait

Reputation: 19805

Why don't you keep them in a list?

threads = []

for val in range(0,max_val):
    thread1 = threading.Thread(target=func9, args=(val,))
    thread1.start()
    threads.append(thread1)

for thread in threads:
    thread.join()

Upvotes: 4

Related Questions