![](/img/trans.png)
[英]Getter on a object inheriting from multiprocessing.Process
[英]Assertion error when inheriting multiprocessing.Process
我需要一個單獨的進程來在初始化時打開一些文件並在最后輕輕關閉它們。 為此,我從Process
繼承了 class 。 這是一個最小的演示:
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()
使用此代碼,我得到一個斷言異常:
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
如果用BaseProcess
替換Process
也會發生同樣的情況。 接下來我在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'
問題是:如何以適當的方式繼承 Process ? 也許我采取的概念是錯誤的?
早些時候,在另一篇文章“ 在 multiprocessing.pool 中,Error group argument must be None for now ”中描述了一個類似的錯誤,但是我沒有看到問題的解決方案。 據我了解,該行為高度依賴於 Python 子版本。 這一點都不酷。
PS:Ubuntu 20.04,Anaconda 3 與 Python 3.7.6。
它應該是super().__init__()
而不是super().__init__(self)
。
super()
在這種情況下轉換為super(Proxy, self)
,已經將超級對象綁定到您的Proxy
實例。 你調用超級對象上的方法,就像你總是使用方法一樣,沒有顯式傳遞self
。
group
是BaseProcess.__init__(self, group=None, target=None...)
中的第二個參數,並且在代碼中調用super().__init__(self)
時,您將其設置為self
,因此是AssertionError
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.