为什么多处理为猴子补丁提高“属性”?

发布于 2025-01-29 11:37:50 字数 2175 浏览 1 评论 0原文

我对多处理中的猴子修补感到困惑。补丁在汇总之前工作,但是方法在池中丢失了。我知道可以通过覆盖或修改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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文