【问题标题】:Python thread synchronisationPython线程同步
【发布时间】:2011-03-13 13:57:29
【问题描述】:

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

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

我有一些关于线程同步的基础,但我不知道如何处理这种情况。 python库中是否有任何内置解决方案我必须编写自己的(基于信号量的?)解决方案?

【问题讨论】:

    标签: python multithreading synchronisation


    【解决方案1】:

    您可以使用join 等待线程:

    # start the two threads
    t1.start()
    t2.start()
    
    # wait until both ended
    t1.join()
    t2.join()
    
    # then start the third
    t3.start()
    

    【讨论】:

      【解决方案2】:

      我建议你看看模块threading。它提供lock objectscondition objectssemaphore objects

      【讨论】: