如何同时加入一个 multiprocessing.Process() 列表? [英] How to join a list of multiprocessing.Process() at the same time?
问题描述
给定运行 multiprocessing.Process
-instances 的 list()
,我如何加入所有实例并在没有 的情况下退出时立即返回Process.join
-超时和循环?
Given a list()
of running multiprocessing.Process
-instances, how can I join on all of them and return as soon as one exits without a Process.join
-timeout and looping?
from multiprocessing import Process
from random import randint
from time import sleep
def run():
sleep(randint(0,5))
running = [ Process(target=run) for i in range(10) ]
for p in running:
p.start()
如何阻塞直到 p
中的至少一个 Process
退出?
How can I block until at least one Process
in p
exits?
我不想做的是:
exit = False
while not exit:
for p in running:
p.join(0)
if p.exitcode is not None:
exit = True
break
推荐答案
您可以使用 multiprocessing.connection.wait()
(Python 3.3+) 一次等待多个 Process.sentinel
.哨兵 将准备就绪,只要一个进程退出并因此解除对 connection.wait()
的阻止.
You can use multiprocessing.connection.wait()
(Python 3.3+) to wait on several Process.sentinel
s at once. A sentinel will become ready, as soon a Process exits and hence unblock the connection.wait()
.
multiprocessing.connection.wait(object_list, timeout=None)
等待 object_list 中的对象准备就绪.返回那些列表object_list 中准备好的对象.如果超时是浮点数,则调用块最多几秒钟.如果超时是 None 那么它将无限期阻止.负超时是等效的到零超时.
Wait till an object in object_list is ready. Returns the list of those objects in object_list which are ready. If timeout is a float then the call blocks for at most that many seconds. If timeout is None then it will block for an unlimited period. A negative timeout is equivalent to a zero timeout.
对于 Unix 和 Windows,一个对象可以出现在 object_list 中,如果它是
For both Unix and Windows, an object can appear in object_list if it is
可读的Connection对象;
a readable Connection object;
一个已连接且可读的socket.socket对象;或
a connected and readable socket.socket object; or
Process 对象的哨兵属性.
the sentinel attribute of a Process object.
当有可用数据时,连接或套接字对象准备就绪从中读取,或者另一端已关闭....
A connection or socket object is ready when there is data available to be read from it, or the other end has been closed. ...
from multiprocessing import Process, connection, current_process
from random import randint
from time import sleep
from datetime import datetime
def run():
sleep(randint(2,10))
print(f"{datetime.now()} {current_process().name} exiting")
if __name__ == '__main__':
pool = [Process(target=run) for _ in range(4)]
for p in pool:
p.start()
print(f"{datetime.now()} {current_process().name} waiting")
connection.wait(p.sentinel for p in pool)
print(f"{datetime.now()} {current_process().name} unblocked")
示例输出:
2019-07-22 21:54:07.061989 MainProcess waiting
2019-07-22 21:54:09.062498 Process-3 exiting
2019-07-22 21:54:09.063565 MainProcess unblocked
2019-07-22 21:54:09.064391 Process-4 exiting
2019-07-22 21:54:14.068392 Process-2 exiting
2019-07-22 21:54:17.062045 Process-1 exiting
Process finished with exit code 0
这篇关于如何同时加入一个 multiprocessing.Process() 列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!