小编ton*_*uan的帖子

Python-超时加入多个线程

我有多个正在运行的Process线程,我想将它们与所有 timeout参数一起加入。我知道,如果没有超时的必要,我可以写:

for thread in threads:
    thread.join()
Run Code Online (Sandbox Code Playgroud)

我想到的一种解决方案是使用将所有线程连接在一起的主线程,然后尝试连接该线程。但是,我在Python中收到以下错误:

AssertionError: can only join a child process
Run Code Online (Sandbox Code Playgroud)

我的代码如下。

def join_all(threads):
    for thread in threads:
        thread.join()

if __name__ == '__main__':
    for thread in threads:
        thread.start()

    master = multiprocessing.Process(target=join_all, args=(threads,))
    master.start()
    master.join(timeout=60)
Run Code Online (Sandbox Code Playgroud)

python multiprocessing

4
推荐指数
2
解决办法
3868
查看次数

标签 统计

multiprocessing ×1

python ×1