Python线程同步

Man*_*duc 2 python multithreading synchronisation

我有3个任务:t1,t2和t3.我想在两个并行线程中运行t1和t2.我想在运行t3之前等待t1和t2执行结束.

t1 =========> |
t2 ====> |
t3 ...................... | =======>
------------------ -------------------------------------------(时间) - >

我有一些关于线程同步的基础,但我无法找到如何管理这种情况.在python库中是否有任何内置解决方案我是否必须编写自己的(基于信号量的?)解决方案?

Joc*_*zel 6

您可以在线程上等待join:

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

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

# then start the third
t3.start()
Run Code Online (Sandbox Code Playgroud)