为什么多处理为猴子补丁提高“属性”?
我对多处理
中的猴子修补感到困惑。补丁在汇总之前工作,但是方法在池中丢失了。我知道可以通过覆盖或修改testClass.test = func
testclass.func = func
from multiprocessing import Pool
class TestClass:
def __init__(self) -> None:
pass
def func(self, a):
print(a)
# monkey patching
TestClass.test = func
print('test monkey')
TestClass().test(True)
pool = Pool()
for i in range(2):
temp = TestClass()
print(f"monkey before pool has test [{bool(hasattr(temp, 'test'))}]")
pool.apply_async(temp.test, args=(i,))
pool.close()
pool.join()
trackback 告诉我func func func func
丢失?
❯ python monky_patch_multiprocessing.py
test monkey
True
monkey before pool has test [True]
monkey before pool has test [True]
Process ForkPoolWorker-1:
Process ForkPoolWorker-2:
Traceback (most recent call last):
File "/path/to/miniconda3/envs/seismic/lib/python3.10/multiprocessing/process.py", line 315, in _bootstrap
self.run()
File "/path/to/miniconda3/envs/seismic/lib/python3.10/multiprocessing/process.py", line 108, in run
self._target(*self._args, **self._kwargs)
File "/path/to/miniconda3/envs/seismic/lib/python3.10/multiprocessing/pool.py", line 114, in worker
task = get()
File "/path/to/miniconda3/envs/seismic/lib/python3.10/multiprocessing/queues.py", line 368, in get
return _ForkingPickler.loads(res)
AttributeError: 'TestClass' object has no attribute 'func'
Traceback (most recent call last):
File "/path/to/miniconda3/envs/seismic/lib/python3.10/multiprocessing/process.py", line 315, in _bootstrap
self.run()
File "/path/to/miniconda3/envs/seismic/lib/python3.10/multiprocessing/process.py", line 108, in run
self._target(*self._args, **self._kwargs)
File "/path/to/miniconda3/envs/seismic/lib/python3.10/multiprocessing/pool.py", line 114, in worker
task = get()
File "/path/to/miniconda3/envs/seismic/lib/python3.10/multiprocessing/queues.py", line 368, in get
return _ForkingPickler.loads(res)
AttributeError: 'TestClass' object has no attribute 'func'
I'm confused about monkey patching in multiprocessing.Pool
. The patching worked before pooling, but the method lost in the pool. I know it could be fixed by overriding or modifying TestClass.test = func
to TestClass.func = func
from multiprocessing import Pool
class TestClass:
def __init__(self) -> None:
pass
def func(self, a):
print(a)
# monkey patching
TestClass.test = func
print('test monkey')
TestClass().test(True)
pool = Pool()
for i in range(2):
temp = TestClass()
print(f"monkey before pool has test [{bool(hasattr(temp, 'test'))}]")
pool.apply_async(temp.test, args=(i,))
pool.close()
pool.join()
The Traceback told me func
lost?
❯ python monky_patch_multiprocessing.py
test monkey
True
monkey before pool has test [True]
monkey before pool has test [True]
Process ForkPoolWorker-1:
Process ForkPoolWorker-2:
Traceback (most recent call last):
File "/path/to/miniconda3/envs/seismic/lib/python3.10/multiprocessing/process.py", line 315, in _bootstrap
self.run()
File "/path/to/miniconda3/envs/seismic/lib/python3.10/multiprocessing/process.py", line 108, in run
self._target(*self._args, **self._kwargs)
File "/path/to/miniconda3/envs/seismic/lib/python3.10/multiprocessing/pool.py", line 114, in worker
task = get()
File "/path/to/miniconda3/envs/seismic/lib/python3.10/multiprocessing/queues.py", line 368, in get
return _ForkingPickler.loads(res)
AttributeError: 'TestClass' object has no attribute 'func'
Traceback (most recent call last):
File "/path/to/miniconda3/envs/seismic/lib/python3.10/multiprocessing/process.py", line 315, in _bootstrap
self.run()
File "/path/to/miniconda3/envs/seismic/lib/python3.10/multiprocessing/process.py", line 108, in run
self._target(*self._args, **self._kwargs)
File "/path/to/miniconda3/envs/seismic/lib/python3.10/multiprocessing/pool.py", line 114, in worker
task = get()
File "/path/to/miniconda3/envs/seismic/lib/python3.10/multiprocessing/queues.py", line 368, in get
return _ForkingPickler.loads(res)
AttributeError: 'TestClass' object has no attribute 'func'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论