简体   繁体   English

是否可以从python多处理模块的进程之间自动共享队列对象?

[英]Is queue object automatically shared among Processes from python multiprocessing module?

I have recently start working with Python multiprocessing module. 我最近开始使用Python多处理模块。 I understand explanation of queues, but recently I found on https://pymotw.com/2/multiprocessing/communication.html that queues don't need to be pass as args to Proccess constructor method, eg 我了解队列的解释,但是最近我在https://pymotw.com/2/multiprocessing/communication.html上发现,不需要将arg作为args传递给Proccess构造函数方法,例如

p = Process(target=f, args=(q,)),

instead, it seems that they are globally shared. 相反,它们似乎是全局共享的。 I thought that this is only the case when we have managed queues, ie 我以为只有在我们管理队列的情况下,即

queue = manager.Queue()

Can someone help me to understand this? 有人可以帮我理解吗?

In Unix, a child process is created with fork() . 在Unix中,使用fork()创建一个子进程。

In Windows, a child process is created by invoking the same script with special arguments . 在Windows中,通过使用特殊参数调用相同的脚本来创建子进程。

In both cases, there may be the q variable in the child process because it inherited the state or because the relevant code has run before execution reached the worker function. 在这两种情况下,子进程中都可能存在q变量,因为它继承了状态,或者因为相关代码在执行到达worker函数之前已经运行。

But that is not enough. 但这还不够。 An IPC needs to be set up between the processes for it to play its role as a communication channel. 需要在各个过程之间建立IPC,以发挥其作为通信渠道的作用。 Otherwise, it's just a regular local object. 否则,它只是一个常规的本地对象。

When in doubt, see the official documentation which is the authoritative information source and is generally of exceptional quality. 如有疑问,请参阅官方文档该文档是权威的信息源,通常具有卓越的质量。 With multiprocessing , it's especially important to stick to the docs because due to its quirky nature, various things may seem to work but break in unpredictable ways. 使用multiprocessing ,坚持使用文档尤为重要,因为由于其古怪的性质,各种事物似乎都可以工作,但会以无法预测的方式中断。

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

相关问题 Python:为什么多进程锁在这里的进程之间共享? - Python: Why is the multiprocessing lock shared among processes here? 在不使用多处理模块的情况下在Python进程之间排队 - Queue between Python processes without using the multiprocessing module 通过Python多处理模块管理流程 - Managing Processes from Python multiprocessing module Python multiprocessing.Queue 未从分叉进程接收放置 - Python multiprocessing.Queue not receiving puts from forked processes python多处理队列不在共享内存中 - python multiprocessing queue is not in shared memory Python多处理:RuntimeError:“只应通过继承在进程之间共享队列对象” - Python multiprocessing: RuntimeError: “Queue objects should only be shared between processes through inheritance” 多处理中共享dict()中的python shared Queue() - python shared Queue() in shared dict() in multiprocessing 如何从 python 中的多处理模块获取当前正在运行的进程名称 - How to get currently running processes names from multiprocessing module in python 在 Python 的多处理模块中打印来自并行进程的数据 - Print data from parallel processes in Python's multiprocessing module RawArray 未被进程修改为 Python 多处理的共享内存 - RawArray not modified by processes as shared memory for Python multiprocessing
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM