简体   繁体   English

是否可以在进程之间传递Python Future对象?

[英]Is it possible to pass Python Future objects between processes?

Based on my experiments I'm guessing the answer to this is no. 根据我的实验,我猜这个答案是否定的。 But perhaps it might be possible with some changes to the futures module. 但也许有可能对期货模块进行一些改变。

I would like to submit a worker that itself creates an executor and submits work. 我想提交一个自己创建执行者并提交工作的工人。 I want to return that second future to the main process. 我想把第二个未来归还给主流程。 I have this MWE, which does not work because the f2 object likely becomes disassociated from its parent executor when it is sent over via multiprocessing. 我有这个MWE,它不起作用,因为当它通过多处理发送时, f2对象可能会与其父执行器解除关联。 (It does work if both executors are ThreadPoolExecutor, because the f2 object is never copied). (如果两个执行程序都是ThreadPoolExecutor,它确实有效,因为永远不会复制f2对象)。

from concurrent.futures import ProcessPoolExecutor, ThreadPoolExecutor
import time

def job1():
    try:
        ex2 = ThreadPoolExecutor()
        time.sleep(2)
        f2 = ex2.submit(job2)
    finally:
        ex2.shutdown(wait=False)
    return f2

def job2():
    time.sleep(2)
    return 'done'

try:
    ex1 = ProcessPoolExecutor()
    f1 = ex1.submit(job1)
finally:
    ex1.shutdown(wait=False)

print('f1 = {!r}'.format(f1))
f2 = f1.result()
print('f1 = {!r}'.format(f1))
print('f2 = {!r}'.format(f2))

My question is: Is there any safe way that I might send a future object across a multiprocessing Pipe and be able to receive the value when it is finished. 我的问题是:是否有任何安全的方法可以在多处理管道中发送未来的对象,并且能够在完成后接收该值。 It seems like I might need to set up another executor-like construct that listens for results over another Pipe. 看起来我可能需要设置另一个类似于执行器的构造,用于侦听另一个管道上的结果。

My current setup is Ubuntu 16.04.5 LTS and Python 3.6.5. 我目前的设置是Ubuntu 16.04.5 LTS和Python 3.6.5。 I received the following error when running the code posted above: 运行上面发布的代码时收到以下错误:

f2 = f1.result()

followed by 其次是

concurrent.futures.process.BrokenProcessPool: A process in the process pool was terminated abruptly while the future was running or pending.

Looking through the Python documentation, I found under 17.4.3. 浏览Python文档,我在17.4.3下找到了。 ProcessPoolExecutor that ProcessPoolExecutor即

"calling Executor or Future methods from a callable submitted to a ProcessPoolExecutor will result in deadlock." “从提交给ProcessPoolExecutor的callable调用Executor或Future方法将导致死锁。”

Further, under 此外,在

class concurrent.futures.ProcessPoolExecutor(max_workers=None)

I also found the following: 我还发现了以下内容:

"changed in version 3.3: when one of the worker processes terminates abruptly, a BrokenProcessPool error is now raised. Previously, behaviour was undefined but operations on the executor or its futures would often freeze or deadlock." “在版本3.3中更改:当其中一个工作进程突然终止时,现在引发了BrokenProcessPool错误。以前,行为未定义,但对执行程序或其未来的操作通常会冻结或死锁。”

Further, by definition the ProcessPoolExecutor turns off the Global Interpreter Lock, opening it to access by other processes. 此外,根据定义,ProcessPoolExecutor关闭全局解释器锁,将其打开以供其他进程访问。

To answer your question on whether any safe way is available to send a future object across a multiprocessing pipe and receive it on the other end, I would say no with the standard library. 要回答关于是否有任何安全方法可以通过多处理管道发送未来对象并在另一端接收它的问题,我会对标准库说不。

References: 参考文献:

https://docs.python.org/3.6/library/concurrent.futures.html#processpoolexecutor https://docs.python.org/3.6/library/concurrent.futures.html#processpoolexecutor

https://docs.python.org/3.6/glossary.html#term-global-interpreter-lock https://docs.python.org/3.6/glossary.html#term-global-interpreter-lock

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM