How to wait till all threads finish their work?

Viewed 11262

I have the following script (don't refer to the contents):

import _thread

def func1(arg1, arg2):
    print("Write to CLI")

def verify_result():
    func1()


for _ in range (4):
    _thread.start_new_thread(func1, (DUT1_CLI, '0'))

verify_result()

I want to concurrently execute (say 4 threads) func1() which in my case includes a function call that can take time to execute. Then, only after the last thread finished its work I want to execute verify_result().

Currently, the result I get is that all threads finish their job, but verify_result() is executed before all threads finish their job.

I have even tried to use the following code (of course I imported threading) under the for loop but that didn't do the work (don't refer to the arguments)

t = threading.Thread(target = Enable_WatchDog, args = (URL_List[x], 180, Terminal_List[x], '0'))
t.start()
t.join()
2 Answers

Your last threading example is close, but you have to collect the threads in a list, start them all at once, then wait for them to complete all at once. Here's a simplified example:

import threading
import time

# Lock to serialize console output
output = threading.Lock()

def threadfunc(a,b):
    for i in range(a,b):
        time.sleep(.01) # sleep to make the "work" take longer
        with output:
            print(i)

# Collect the threads
threads = []
for i in range(10,100,10):
    # Create 9 threads counting 10-19, 20-29, ... 90-99.
    thread = threading.Thread(target=threadfunc,args=(i,i+10))
    threads.append(thread)

# Start them all
for thread in threads:
    thread.start()

# Wait for all to complete
for thread in threads:
    thread.join()
Related