Python multiprocessing: RuntimeError: quot;Queue objects should only be shared between processes through inheritancequot;(Python 多处理:RuntimeError:“队列对象只能通过继承在进程之间共享)
问题描述
我知道 multiprocessing.Manager() 以及如何使用它来创建共享对象.特别是可以在工作人员之间共享的队列.有这个 问题,这个问题,这个问题.
I am aware of multiprocessing.Manager() and how it can be used to create shared objects. In particular, queues which can be shared among workers. There is this question, this question, and this question.
但是,这些链接没有提到为什么我们可以使用继承在进程之间共享.据我了解,仍然只能在这种情况下复制队列.
However, these links don't mention why we can use inheritance for sharing between processes. As I understand, a queue can still only be copied in this case.
推荐答案
python 中的 Queue
实现依赖于系统 pipe
将数据从一个进程传输到另一个进程和一些 semaphores
来保护这个 pipe
上的读写.
The Queue
implementation in python relies on a system pipe
to transmit the data from one process to another and some semaphores
to protect the read and write on this pipe
.
管道
在进程中作为打开文件处理,并且由于操作系统限制,只能在生成时与子进程共享.semaphores
也被视为只应在生成时共享的文件,至少在基于 UNIX 的系统中,对于早期版本的 python.
The pipe
is handled as an open file in the process and can only be shared with a subprocess at spawning time, because of OS constraints.
The semaphores
are also treated as files that should only be shared at spawning time, at least in UNIX based system, for early version of python.
由于这两个子对象一般不能共享,Queue
一旦启动就不能被pickle并发送到子进程.
As these 2 sub objects cannot be shared in general, the Queue
cannot be pickled and sent to a subprocess once it has been started.
但是,对于某些操作系统和最新版本的 python,可以共享 Connection
并创建可共享的 Semaphore
.因此,理论上您可以创建自己的 Queue
可以在进程之间共享.但它涉及很多黑客攻击,可能不是很安全.
However, for some OS and recent version of python, it is possible to share the Connection
and to create sharable Semaphore
. Thus, you could in theory create your own Queue
that can be shared between processes. But it involves a lot of hacks and might not be very secured.
这篇关于Python 多处理:RuntimeError:“队列对象只能通过继承在进程之间共享"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Python 多处理:RuntimeError:“队列对象只能通过继承在进程之间共享"


- 如何使用PYSPARK从Spark获得批次行 2022-01-01
- YouTube API v3 返回截断的观看记录 2022-01-01
- 使用公司代理使Python3.x Slack(松弛客户端) 2022-01-01
- 使用 Cython 将 Python 链接到共享库 2022-01-01
- 我如何卸载 PyTorch? 2022-01-01
- 计算测试数量的Python单元测试 2022-01-01
- CTR 中的 AES 如何用于 Python 和 PyCrypto? 2022-01-01
- 我如何透明地重定向一个Python导入? 2022-01-01
- ";find_element_by_name(';name';)";和&QOOT;FIND_ELEMENT(BY NAME,';NAME';)";之间有什么区别? 2022-01-01
- 检查具有纬度和经度的地理点是否在 shapefile 中 2022-01-01