简体   繁体   English

如何从 multiprocessing.Pipe 继承?

[英]How to inherit from multiprocessing.Pipe?

I want to build a wrapper class around the Python's multiprocessing.Pipe object.我想围绕 Python 的multiprocessing.Pipe对象构建一个包装类。 Inheriting from the multiprocessing module is known to have its challenges (see here for a similar question discussing inheritance for multiprocessing.Queue ), and I'm facing an error I can't resolve.众所周知,从multiprocessing模块继承有其挑战(有关讨论multiprocessing.Queue继承的类似问题,请参见此处),并且我面临无法解决的错误。

Let's consider a simple example:让我们考虑一个简单的例子:

from multiprocessing import Pipe


class MyClass1: pass


class MyClass2(Pipe, MyClass1):
    pass

Here we create a class MyClass2 that inherits both from the Pipe class and from a custom class MyClass1 .在这里,我们创建了一个类MyClass2 ,它继承自 Pipe 类和自定义类MyClass1 Running the above will raise运行以上将提高

TypeError: metaclass conflict: the metaclass of a derived class
 must be a (non-strict) subclass of the metaclasses of all its bases

I know that to solve this error I will likely have to introduce a metaclass that MyClass2 can inherit from, but when I try to determine the metaclass of the Pipe object using type(Pipe) I get <class 'type'> which doesn't help at all.我知道要解决这个错误,我可能不得不引入一个MyClass2可以继承的元类,但是当我尝试使用type(Pipe)确定Pipe对象的元类时,我得到<class 'type'>帮助。

The Stack Overflow questions I linked to above mentions that there is essentially a special way to inherit Queue .我在上面链接的 Stack Overflow 问题提到本质上有一种特殊的方式来继承Queue Is there also a special way for Pipe ? Pipe也有特殊的方法吗? Any suggestions on how to avoid this error?有关如何避免此错误的任何建议?

There is no Pipe class.没有Pipe类。 It works for multiprocessing.Queue because there is a multiprocessing.queues.Queue class, but multiprocessing.Pipe is just backed by another Pipe function in multiprocessing.connection.py .它适用于multiprocessing.Queue因为有multiprocessing.queues.Queue类,但是multiprocessing.Pipe只是另一个支持Pipe在功能上multiprocessing.connection.py

You are trying to inherit from a function which always returns two connection objects.您正在尝试从始终返回两个连接对象的函数继承。 You would rather have to subclass the various connections objects Pipe can return.您宁愿必须对Pipe可以返回的各种连接对象进行子类化。 What connection objects this will be is OS-dependent and dependent on if the connection should be duplex/simplex.这将是什么连接对象取决于操作系统并取决于连接是否应该是双工/单工。 I doubt that would be a good idea, though.不过,我怀疑这会是个好主意。 (Take a look at multiprocessing.connection.py to get cured from that idea ;) (看看multiprocessing.connection.py从这个想法中得到治愈;)

I would suggest to make your own MyPipe function, let it call multiprocessing.Pipe internally and let it append, whatever you need, to the returned connection objects.我建议创建您自己的MyPipe函数,让它在内部调用multiprocessing.Pipe并让它根据您的需要附加到返回的连接对象。

From python documentation.来自 python 文档。 https://docs.python.org/3.7/library/multiprocessing.html https://docs.python.org/3.7/library/multiprocessing.html

The Pipe() function returns a pair of connection objects connected by a pipe which by default is duplex (two-way). Pipe() 函数返回由管道连接的一对连接对象,管道默认为双工(双向)。

Pipe is a method and therefore you cannot inherit from it. Pipe是一种方法,因此您不能继承它。

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

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