Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to wait till all threads finish their work?

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()
like image 902
Moshe S. Avatar asked Jan 30 '23 13:01

Moshe S.


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()
like image 149
Mark Tolonen Avatar answered Feb 02 '23 11:02

Mark Tolonen


Say you have a list of threads. You loop(each_thread) over them -

for each_thread in thread_pool:
    each_thread.start()

within the loop to start execution of the run function within each thread.

The same way, you write another loop after you start all threads and have

for each_thread in thread_pool:
    each_thread.join()

what join does is that it will wait for thread i to finish execution before letting i+1th thread to finish execution.

The threads would run concurrently, join() would just synchronize the way each thread returns its results.

In your case specifically, you can the join() loop and the run verify_result() function.

like image 41
rohit keshav Avatar answered Feb 02 '23 11:02

rohit keshav