Python 进程池非守护进程?
是否可以创建一个非守护进程的 python 池?我希望一个池能够调用内部有另一个池的函数。
我想要这个,因为守护进程无法创建进程。具体来说,它会导致错误:
AssertionError: daemonic processes are not allowed to have children
例如,考虑这样一种情况:function_a
有一个运行 function_b
的池,而 function_b
有一个运行 function_c
的池。该函数链将失败,因为 function_b
正在守护进程中运行,而守护进程无法创建进程。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
multiprocessing.pool.Pool
类在其__init__
方法中创建工作进程,使它们成为守护进程并启动它们,并且无法重新设置它们的daemon
属性在启动之前设置为False
(此后不再允许)。但是您可以创建自己的multiprocessing.pool.Pool
子类(multiprocessing.Pool
只是一个包装函数)并替换您自己的multiprocessing.Process< /code> 子类,始终是非守护进程,用于工作进程。
这是如何执行此操作的完整示例。重要的部分是顶部的两个类
NoDaemonProcess
和MyPool
以及调用pool.close()
和pool.join( )
最后在您的MyPool
实例上。The
multiprocessing.pool.Pool
class creates the worker processes in its__init__
method, makes them daemonic and starts them, and it is not possible to re-set theirdaemon
attribute toFalse
before they are started (and afterwards it's not allowed anymore). But you can create your own sub-class ofmultiprocesing.pool.Pool
(multiprocessing.Pool
is just a wrapper function) and substitute your ownmultiprocessing.Process
sub-class, which is always non-daemonic, to be used for the worker processes.Here's a full example of how to do this. The important parts are the two classes
NoDaemonProcess
andMyPool
at the top and to callpool.close()
andpool.join()
on yourMyPool
instance at the end.我有必要在 Python 3.7 中使用非守护进程池,并最终调整了已接受答案中发布的代码。下面是创建非守护进程池的代码片段:
由于
multiprocessing
的当前实现已被广泛重构为基于上下文,我们需要提供一个NoDaemonContext
类来有我们的NoDaemonProcess
作为属性。然后,NestablePool
将使用该上下文而不是默认上下文。也就是说,我应该警告这种方法至少有两个警告:
multiprocessing
包的实现细节,因此可能随时中断。多处理
使得使用非守护进程变得如此困难,这是有充分理由的,其中许多原因都已解释此处。我认为最引人注目的是:I had the necessity to employ a non-daemonic pool in Python 3.7 and ended up adapting the code posted in the accepted answer. Below there's the snippet that creates the non-daemonic pool:
As the current implementation of
multiprocessing
has been extensively refactored to be based on contexts, we need to provide aNoDaemonContext
class that has ourNoDaemonProcess
as attribute.NestablePool
will then use that context instead of the default one.That said, I should warn that there are at least two caveats to this approach:
multiprocessing
package, and could therefore break at any time.multiprocessing
made it so hard to use non-daemonic processes, many of which are explained here. The most compelling in my opinion is:从 Python 3.8 开始,
concurrent.futures。 ProcessPoolExecutor
没有这个限制。它可以有一个嵌套的进程池,完全没有问题:上面的演示代码是用Python 3.8测试的。
然而,
ProcessPoolExecutor
的限制是它没有maxtasksperchild
。如果您需要这个,请考虑Massimiliano 的回答。信用:jfs 的回答
As of Python 3.8,
concurrent.futures.ProcessPoolExecutor
doesn't have this limitation. It can have a nested process pool with no problem at all:The above demonstration code was tested with Python 3.8.
A limitation of
ProcessPoolExecutor
, however, is that it doesn't havemaxtasksperchild
. If you need this, consider the answer by Massimiliano instead.Credit: answer by jfs
multiprocessing 模块有一个很好的界面,可以将池与进程或< /strong> 线程。根据您当前的用例,您可能会考虑对外部池使用
multiprocessing.pool.ThreadPool
,这将导致线程(允许从内部生成进程)反对流程。它可能受到 GIL 的限制,但在我的特定情况下(我测试了两者),创建时外部
池
的进程的启动时间这里远远超过了解决方案线程池
。将
进程
替换为线程
确实很容易。详细了解如何使用ThreadPool
解决方案此处或此处。The multiprocessing module has a nice interface to use pools with processes or threads. Depending on your current use case, you might consider using
multiprocessing.pool.ThreadPool
for your outer Pool, which will result in threads (that allow to spawn processes from within) as opposed to processes.It might be limited by the GIL, but in my particular case (I tested both), the startup time for the processes from the outer
Pool
as created here far outweighed the solution withThreadPool
.It's really easy to swap
Processes
forThreads
. Read more about how to use aThreadPool
solution here or here.在某些 Python 版本上,将标准池替换为自定义池可能会引发错误:
AssertionError:组参数现在必须为 None
。在这里我找到了一个可以提供帮助的解决方案:
On some Python versions replacing standard Pool to custom can raise error:
AssertionError: group argument must be None for now
.Here I found a solution that can help:
我见过有人通过使用
celery
的multiprocessing
分支来处理这个问题,称为 台球(多处理池扩展),它允许守护进程生成子进程。解决方法是简单地通过以下方式替换multiprocessing
模块:I have seen people dealing with this issue by using
celery
's fork ofmultiprocessing
called billiard (multiprocessing pool extensions), which allows daemonic processes to spawn children. The walkaround is to simply replace themultiprocessing
module by:我遇到的问题是尝试在模块之间导入全局变量,导致 ProcessPool() 行被多次评估。
globals.py
然后从代码中的其他位置安全地导入
我在这里围绕
pathos.multiprocessing
编写了一个更扩展的包装类:作为旁注,如果您的用例只需要异步多进程映射作为性能优化,然后 joblib 将在幕后管理所有进程池并允许使用这种非常简单的语法:
The issue I encountered was in trying to import globals between modules, causing the ProcessPool() line to get evaluated multiple times.
globals.py
Then import safely from elsewhere in your code
I have written a more expanded wrapper class around
pathos.multiprocessing
here:As a side note, if your usecase just requires async multiprocess map as a performance optimization, then joblib will manage all your process pools behind the scenes and allow this very simple syntax:
即使您已经处于守护进程中,以下也是启动池的方法。这是在 python 3.8.5 中测试的。
首先,定义 Undaemonize 上下文管理器,它临时删除当前进程的守护进程状态。
现在,您可以按如下方式启动池,甚至可以在守护进程中启动:
虽然此处的其他方法旨在首先创建非守护进程的池,但此方法允许您启动池,即使您处于守护进程中已经。
Here is how you can start a pool, even if you are in a daemonic process already. This was tested in python 3.8.5
First, define the
Undaemonize
context manager, which temporarily deletes the daemon state of the current process.Now you can start a pool as follows, even from within a daemon process:
While the other approaches here aim to create pool that is not daemonic in the first place, this approach allows you to start a pool even if you are in a daemonic process already.
当错误看似误报时,这提供了一种解决方法。正如 James 所指出的,从守护进程中无意导入可能会发生这种情况。
例如,如果您有以下简单代码,
WORKER_POOL
可能会无意中从工作线程导入,从而导致错误。一个简单但可靠的解决方法是:
在上面的解决方法中,可以使用
MyClass.worker_pool
而不会出现错误。如果您认为这种方法可以改进,请告诉我。This presents a workaround for when the error is seemingly a false-positive. As also noted by James, this can happen to an unintentional import from a daemonic process.
For example, if you have the following simple code,
WORKER_POOL
can inadvertently be imported from a worker, leading to the error.A simple but reliable approach for a workaround is:
In the above workaround,
MyClass.worker_pool
can be used without the error. If you think this approach can be improved upon, let me know.从 Python 3.7 版本开始,我们可以使用
if __name__ == "__main__":
创建非守护进程 ProcessPoolExecutor,这在使用多处理时是必要的。
Since Python version 3.7 we can create non-daemonic ProcessPoolExecutor
Using
if __name__ == "__main__":
is necessary while using multiprocessing.对我来说,这个答案有帮助: https://stackoverflow.com/a/71929459/14715428
虽然问题本身有点不同,速度保持不变,我不需要重写任何东西
For me it was that answer that helped: https://stackoverflow.com/a/71929459/14715428
Although the question itself is a little bit different, the speed remained the same and I didn't have to rewrite anything