2

I needed a separate process that would open some files on initialization and close them gently at the end. For this, I inherited a class from Process. Here is a minimal demo:

from multiprocessing import Process
from multiprocessing.process import BaseProcess

class Proxy(Process):
    def __init__(self):
        super().__init__(self)
    def run(self):
        pass

if __name__ == "__main__":
    proxy = Proxy()
    proxy.start()
    proxy.join()

With this code I get an assertion exception:

Traceback (most recent call last):
  File "mp_proxy.py", line 11, in <module>
    proxy = Proxy()
  File "mp_proxy.py", line 6, in __init__
    super().__init__(self)
  File "/home/user/opt/anaconda3/lib/python3.7/multiprocessing/process.py", line 74, in __init__
    assert group is None, 'group argument must be None for now'
AssertionError: group argument must be None for now

The same happens if to replace Process with BaseProcess. Next I added a debug print into the process.py, to the BaseProcess.__init__ function, just to look at the group variable, and then I got something different:

multiprocessing.process : Traceback (most recent call last):
  File "mp_proxy.py", line 11, in <module>
    proxy = Proxy()
  File "mp_proxy.py", line 6, in __init__
    super().__init__(self)
  File "/home/user/opt/anaconda3/lib/python3.7/multiprocessing/process.py", line 74, in __init__
    print(__name__, ":", group)
  File "/home/user/opt/anaconda3/lib/python3.7/multiprocessing/process.py", line 254, in __repr__
    elif self._closed:
AttributeError: 'Proxy' object has no attribute '_closed'

The question is: How to inherit Process in a proper way? Maybe the concept I took is wrong?

Earlier, in another post 'Error group argument must be None for now in multiprocessing.pool' a similar error was described, however I did not see a solution to the problem. As far as I understood, the behavior is highly dependent on the Python sub-version. It's not cool at all.

P.S.: Ubuntu 20.04, Anaconda 3 with Python 3.7.6.

Darkonaut
  • 20,186
  • 7
  • 54
  • 65

1 Answers1

3

It should be super().__init__() instead of super().__init__(self).

super() in this case translates to super(Proxy, self), already binding the super-object to your Proxy-instance. You call methods on the super-object like you always do with methods, without explicitly passing self.

group is the second parameter in BaseProcess.__init__(self, group=None, target=None...) and with calling super().__init__(self) in your code, you're setting it to self, hence the AssertionError.

Darkonaut
  • 20,186
  • 7
  • 54
  • 65
  • Such a stupid mistake. Thank you! However, I can't interpret the second error, when I tried the debug output: `AttributeError: 'Proxy' object has no attribute '_closed'` – Evgeny P. Kurbatov Nov 27 '20 at 11:43
  • @EvgenyP.Kurbatov I didn't manage to replicate this particular error, but anyway, `multiprocessing.process.BaseProcess` isn't complete and ready to be subclassed by users. Use `multiprocessing.Process` (`multiprocessing.context.Process`) instead. – Darkonaut Nov 27 '20 at 12:20