简体   繁体   中英

How to inherit from multiprocessing.Pipe?

I want to build a wrapper class around the Python's multiprocessing.Pipe object. 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.

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 . 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.

The Stack Overflow questions I linked to above mentions that there is essentially a special way to inherit Queue . Is there also a special way for Pipe ? Any suggestions on how to avoid this error?

There is no Pipe class. 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 .

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. 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 ;)

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.

From python documentation. 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 is a method and therefore you cannot inherit from it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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