gpt4 book ai didi

python - 如何同时加入一个 multiprocessing.Process() 列表?

转载 作者:太空宇宙 更新时间:2023-11-04 11:15:19 25 4
gpt4 key购买 nike

给定一个正在运行的 multiprocessing.Process 实例的 list(),我如何才能加入所有实例并在没有 的情况下退出时立即返回Process.join-超时和循环?

例子

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 退出之前,我该如何阻塞?

我不想做的是:

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.sentinelsentinel将准备就绪,一旦进程退出并因此解锁 connection.wait()

multiprocessing.connection.wait(object_list, timeout=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.

For both Unix and Windows, an object can appear in object_list if it is

  • a readable Connection object;

  • a connected and readable socket.socket object; or

  • 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

关于python - 如何同时加入一个 multiprocessing.Process() 列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57152073/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com