简体   繁体   English

继承 multiprocessing.Process 时的断言错误

[英]Assertion error when inheriting multiprocessing.Process

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 .为此,我从Process继承了 class 。 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 .如果用BaseProcess替换Process也会发生同样的情况。 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:接下来我在process.py中添加了一个调试打印到BaseProcess.__init__ function 中,只是为了查看group变量,然后我得到了一些不同的东西:

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?问题是:如何以适当的方式继承 Process ? 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.早些时候,在另一篇文章“ 在 multiprocessing.pool 中,Error group argument must be None for now ”中描述了一个类似的错误,但是我没有看到问题的解决方案。 As far as I understood, the behavior is highly dependent on the Python sub-version.据我了解,该行为高度依赖于 Python 子版本。 It's not cool at all.这一点都不酷。

PS: Ubuntu 20.04, Anaconda 3 with Python 3.7.6. PS:Ubuntu 20.04,Anaconda 3 与 Python 3.7.6。

It should be super().__init__() instead of super().__init__(self) .它应该是super().__init__()而不是super().__init__(self)

super() in this case translates to super(Proxy, self) , already binding the super-object to your Proxy -instance.super()在这种情况下转换为super(Proxy, self) ,已经将超级对象绑定到您的Proxy实例。 You call methods on the super-object like you always do with methods, without explicitly passing self .调用超级对象上的方法,就像你总是使用方法一样,没有显式传递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 . groupBaseProcess.__init__(self, group=None, target=None...)中的第二个参数,并且在代码中调用super().__init__(self)时,您将其设置为self ,因此是AssertionError

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

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