简体   繁体   English

类型错误:AutoProxy object 不可迭代 - 多处理

[英]TypeError: AutoProxy object is not iterable - multiprocessing

consider the following server code:考虑以下服务器代码:

from multiprocessing.managers import BaseManager, BaseProxy

def baz(aa) :
    print "aaa"
    l = []
    for i in range(3) :
      l.append(aa)
    return l

class SolverManager(BaseManager): pass

manager = SolverManager(address=('127.0.0.1', 50000), authkey='mpm')
manager.register('solver', baz)

server = manager.get_server()
server.serve_forever()

and the associated client:和相关的客户:

import sys
from multiprocessing.managers import BaseManager, BaseProxy

class SolverManager(BaseManager): pass

def main(args) :
    SolverManager.register('solver')
    m = SolverManager(address=('127.0.0.1', 50000), authkey='mpm')
    m.connect()

    for i in m.solver(args[1]):
        print i

if __name__ == '__main__':
    sys.exit(main(sys.argv))

I think I'm missing something important here.我想我在这里遗漏了一些重要的东西。 My guess is that I have to subclass the BaseProxy class to provide an iterable object, but so far I haven't managed to get it right.我的猜测是我必须将 BaseProxy class 子类化以提供可迭代的 object,但到目前为止我还没有成功。

when I run the client I get this error:当我运行客户端时出现此错误:

Traceback (most recent call last):
  File "mpmproxy.py", line 17, in <module>
    sys.exit(main(sys.argv))
  File "mpmproxy.py", line 13, in main
    for i in m.solver(args[1]):
TypeError: 'AutoProxy[solver]' object is not iterable

however if I try to print it, the list is there... Maybe it has also something to do with the way data is serialized between client and server...但是,如果我尝试打印它,列表就在那里......也许它也与数据在客户端和服务器之间序列化的方式有关......

in the documentation there is a similar case (with a generator) and they use the following class to access the data:在文档中有一个类似的案例(使用生成器),他们使用以下 class 来访问数据:

class GeneratorProxy(BaseProxy):
    _exposed_ = ('next', '__next__')
    def __iter__(self):
        return self
    def next(self):
        return self._callmethod('next')
    def __next__(self):
        return self._callmethod('__next__')

shall I do something similar?我应该做类似的事情吗? Can anybody give me an example and explain to me how this works?谁能给我一个例子并向我解释这是如何工作的?

update更新

To clarify: suppose I add the class:澄清一下:假设我添加了 class:

class IteratorProxy(BaseProxy):
    def __iter__(self):
        print self
        return self

and in the client I register the function as在客户端中,我将 function 注册为

SolverManager.register('solver', proxytype=IteratorProxy)

the error I get is:我得到的错误是:

$python mpmproxy.py test
['test', 'test', 'test']
Traceback (most recent call last):
  File "mpmproxy.py", line 22, in <module>
    sys.exit(main(sys.argv))
  File "mpmproxy.py", line 18, in main
    for i in m.solver(args[1]):
TypeError: iter() returned non-iterator of type 'IteratorProxy'

I have the impression I'm missing something stupid here...我的印象是我在这里遗漏了一些愚蠢的东西......

update 2更新 2

I think I solved this problem:我想我解决了这个问题:

The point was to get the real value:关键是要获得真正的价值:

for i in m.solver(args[1])._getvalue():
    print i

gosh... I'm not sure if this is the correct answer or just a workaround ...天哪...我不确定这是正确答案还是只是一种解决方法...

实际上,为了可迭代,你的类需要定义BaseProxy定义的__iter__方法,所以我想继承是正确的方法!

While the solution in OPs 2nd update may work, the method _getvalue() is private and therefore shouldn't really be accessed in this way.虽然 OP 第二次更新中的解决方案可能有效,但方法_getvalue()是私有的,因此不应以这种方式真正访问。 Why don't you try using the list proxy type instead ( from multiprocessing.managers import ListProxy )?为什么不尝试使用列表代理类型( from multiprocessing.managers import ListProxy )? While the result returned from the server still won't be iterable, as far as I've seen, you should be able to run the well known .pop() or .pop(i) (where i is an index) on it, which is a publicly available method for the result.虽然从服务器返回的结果仍然不可迭代,但据我所知,您应该能够在其上运行众所周知的.pop().pop(i) (其中 i 是索引) ,这是一种公开可用的结果方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM