gpt4 book ai didi

python - 为什么在尝试使用 SharedMemoryManager (python 3.8) 替代 BaseManager 时会出现 NameError 错误?

转载 作者:行者123 更新时间:2023-12-02 09:01:00 25 4
gpt4 key购买 nike

Python 3.8 引入了新的共享内存功能。我们正在尝试使用 SharedMemoryManager 并抛出 NameError

我认为我们可能在复杂的场景中做错了什么,所以我使用 python 文档片段将其分解。

try:
# python >= 3.8
from multiprocessing.managers import SharedMemoryManager as Manager
except:
# python < 3.8
from multiprocessing.managers import BaseManager as Manager

class MathsClass:
def add(self, x, y):
return x + y
def mul(self, x, y):
return x * y

class MyManager(Manager):
pass

MyManager.register('Maths', MathsClass)

if __name__ == '__main__':
with MyManager() as manager:
maths = manager.Maths()
print(maths.add(4, 3)) # prints 7
print(maths.mul(7, 8)) # prints 56

这几乎取自多处理文档(除了后备导入),在 python 3.7 中工作正常,但在 python 3.8 中抛出以下错误:

Traceback (most recent call last):
File "scripts/debug_shared_memory_issue.py", line 21, in <module>
maths = manager.Maths()
File "/usr/lib/python3.8/multiprocessing/managers.py", line 740, in temp
token, exp = self._create(typeid, *args, **kwds)
File "/usr/lib/python3.8/multiprocessing/managers.py", line 625, in _create
id, exposed = dispatch(conn, None, 'create', (typeid,)+args, kwds)
File "/usr/lib/python3.8/multiprocessing/managers.py", line 91, in dispatch
raise convert_to_error(kind, result)
multiprocessing.managers.RemoteError:
---------------------------------------------------------------------------
Traceback (most recent call last):
File "/usr/lib/python3.8/multiprocessing/managers.py", line 210, in handle_request
result = func(c, *args, **kwds)
File "/usr/lib/python3.8/multiprocessing/managers.py", line 1312, in create
if hasattr(self.registry[typeid][-1], "_shared_memory_proxy"):
NameError: name 'self' is not defined
---------------------------------------------------------------------------

Python 3.8 的发行说明和文档称 SharedMemoryManager 是“BaseManager 的子类”,因此我们希望它能够作为直接替代品。但似乎并非如此。我们做错了什么?查看 CPython 当前的 3.8 分支,似乎没有相关的更改。在 Python 3.9 中,create 函数中有一个显式的 self 参数。但 3.9 是 WIP,所以我们不想在生产软件中使用它。

感谢您的帮助!

最佳答案

这是 python/cpython@142566c 中修复的错误(v3.9.0a1)。

您可以从python/cpython/blob/v3.9.0a1/Lib/multiprocessing/managers.py#L1269-L1277打补丁:

from multiprocessing.managers import SharedMemoryManager as Manager

import sys

if sys.version_info < (3, 9):
from multiprocessing.managers import Server, SharedMemoryServer

def create(self, c, typeid, /, *args, **kwargs):
if hasattr(self.registry[typeid][-1], "_shared_memory_proxy"):
kwargs['shared_memory_context'] = self.shared_memory_context
return Server.create(self, c, typeid, *args, **kwargs)

SharedMemoryServer.create = create

关于python - 为什么在尝试使用 SharedMemoryManager (python 3.8) 替代 BaseManager 时会出现 NameError 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59172691/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com