Simultaneus线程

Nic*_*son -1 python multithreading

好.也许你因为没有表现出我尝试过的东西而生我的气.但我真的很困惑线程.同时运行三个线程并在它们全部运行完第四个线程之后最简单的可行方法是什么?这将在一个wx应用程序内部运行,所以我想要一种不会锁定我的程序的方法.

编辑:嗯,第四个不需要是一个线程,但它是一个需要在所有线程完成后运行的方法

Tho*_*s K 5

我没有测试过这个,但基本的想法是这样的:

from threading import Thread

threads = [Thread(target=f1), Thread(target=f2), Thread(target=f3)]

for thread in threads:
    thread.start()

# Wait for all of them
for thread in threads:
    thread.join()

# Do stuff afterwards
Run Code Online (Sandbox Code Playgroud)

请注意,由于GIL,线程无法并行执行计算,因此您可能需要使用多处理.但是,如果他们正在等待数据库或网络服务,他们将按预期工作.