Manuel Leduc
Manuel Leduc

Reputation: 1919

Python thread synchronisation

I have 3 tasks : t1, t2 and t3. I want to run t1 and t2 in two parallels threads. And I want to wait for the end of t1 and t2 execution before running t3.

t1 =========> |
t2 ====>           |
t3......................|=======>
-------------------------------------------------------------(time)-->

I have some basis about threads synchronization but I can't find out how to manage with this case. Is there any build-in solution in python library did I have to write a my own (semaphore based ?) solution ?

Upvotes: 2

Views: 313

Answers (2)

Jochen Ritzel
Jochen Ritzel

Reputation: 107638

You can wait on threads with join:

# start the two threads
t1.start()
t2.start()

# wait until both ended
t1.join()
t2.join()

# then start the third
t3.start()

Upvotes: 6

Morten Kristensen
Morten Kristensen

Reputation: 7613

I would advise you to have a look at the module threading. It provides lock objects, condition objects and semaphore objects.

Upvotes: 1

Related Questions