gpt4 book ai didi

Python 多处理传递引用

转载 作者:太空宇宙 更新时间:2023-11-03 11:32:47 27 4
gpt4 key购买 nike

我正在尝试使用 multiprocessing 模块在进程之间传递自定义 Queue。问题是我没有将所有方法都传到另一边。根据文档。

A proxy object has methods which invoke corresponding methods of its referent (although not every method of the referent will necessarily be available through the proxy). A proxy can usually be used in most of the same ways that its referent can

但它没有说明原因或解决方法。我想知道是否有人知道这样做的方法。

这是我正在尝试做的一个小例子。

服务器:

from multiprocessing.managers import BaseManager
from Queue import Queue


class KeyHandler(Queue):

def __init__(self, elements=[]):
Queue.__init__(self)

for element in elements:
self.put(element)

def __iter__(self):
return iter(self.get, object())

def __call__(self):
return self


class QueueManager(BaseManager):
pass

keyhandler = KeyHandler(range(10))


QueueManager.register('keyhandler', callable=keyhandler)
manager = QueueManager(address=('', 50000), authkey='foobar')
server = manager.get_server()
server.serve_forever()

客户:

from multiprocessing.managers import BaseManager


class QueueManager(BaseManager):
pass


QueueManager.register('keyhandler')
manager = QueueManager(address=('', 50000), authkey='foobar')
manager.connect()

keyhandler = manager.keyhandler()

for elem in keyhandler:
print elem

回溯:

Traceback (most recent call last):
File "client2.py", line 14, in <module>
for elem in keyhandler:
TypeError: 'AutoProxy[keyhandler]' object is not iterable

__call__ 方法有效,但 __iter__ 方法无效。我能以某种方式强制/解决这个问题吗?

最佳答案

使用代理。在客户端添加此类声明(连同导入 BaseProxy):

class KeyHandlerProxy(BaseProxy):
_exposed_ = ('next', 'get')

def __iter__(self):
return self

def next(self):
o = self._callmethod('get')
if object() == o:
raise StopIteration
return o

并将register(客户端的)更改为:

QueueManager.register('keyhandler', proxytype=KeyHandlerProxy)

关于Python 多处理传递引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14324069/

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