Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 同时线程_Python_Multithreading - Fatal编程技术网

Python 同时线程

Python 同时线程,python,multithreading,Python,Multithreading,嗯。也许你会因为我没有表现出我的努力而生我的气。但我真的对线程感到困惑。同时运行三个线程并在它们全部完成后运行第四个线程,最简单的方法是什么?这将在wx应用程序中运行,因此我希望有一种不会锁定我的程序的方法 编辑:嗯,第四个不需要是一个线程,但它是一个需要在所有线程完成后运行的方法我还没有测试过这个,但基本思想是这样的: from threading import Thread threads = [Thread(target=f1), Thread(target=f2), Thread(ta

嗯。也许你会因为我没有表现出我的努力而生我的气。但我真的对线程感到困惑。同时运行三个线程并在它们全部完成后运行第四个线程,最简单的方法是什么?这将在wx应用程序中运行,因此我希望有一种不会锁定我的程序的方法


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

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

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

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

这确实是我见过的最好的示例。大多数问题都过于复杂,你真的理解了我在问题中的要求。@NiclasNilsson:很高兴这有帮助!