How to inherit from multiprocessing.Pipe?

Viewed 392

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?

2 Answers

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.

Related