KRISHNA I
KRISHNA I

Reputation: 67

How to wait till all threads are executed without using thread.join()?

I am working on a code where threads are generated in a recursive function. So its tedious to follow all threads. Thread.join() is not feasible. You can do it but with a lot of effort. What I want is to wait till all threads(child process) are completed, before printing result generated after running threads. I guess you can get the gist of the problem. All I want is to execute a statement just before exiting the main program.

Upvotes: 0

Views: 2330

Answers (1)

djoffe
djoffe

Reputation: 112

Did you try using the concurrent.futures package?

You can instantiate a ThreadPoolExecutor and start your threads by submitting to it. Then call the executor's shutdown(wait=True) function to wait for all threads to complete.

Alternatively, use a with ThreadPoolExecutor as e: statement. When you exit the with block, all your threads are completed.

Upvotes: 3

Related Questions