如何从注册到multiprocessing.manager的函数返回两个管道?

时间:2011-10-30 23:44:12

标签: python multiprocessing

我已经玩了一段时间,感觉就像我一样,不停地撞到墙上,前两个工作有3个不同的代码,第二个也应该遵循逻辑。

这位经理:

from multiprocessing.managers import BaseManager
def getAB(): return 'spam', 'eggs'
class MyManager(BaseManager): pass
MyManager.register('getAB', callable=getAB)
m = MyManager(address=('', 50000), authkey='A')
s = m.get_server()
s.serve_forever()

和这个客户:

from multiprocessing.managers import BaseManager
class MyManager(BaseManager): pass
MyManager.register('getAB')
manager = QueueManager(address=('127.0.0.1', 50000), authkey='A')
manager.connect()
a,b = manager.getAB()._getvalue()
print "a", a
print "b", b

一起工作并输出

a spam
b eggs

将getAB更改为此getPipe,并通过线程化serve_forever获取这些更改

pipeServer, pipeClient = Pipe()
def getPipe(): return pipeClient

t = Thread(target=s.serve_forever)
t.daemon = True
t.start()

print "pipe", pipeServer.recv()

并将客户端的结尾更改为此

pipe = manager.getPipe()
pipe.send("spam")

给出

pipe spam

现在把它们放在一起似乎合乎逻辑地假设你可以有一个函数getPipes返回两个管道这是我的尝试:

pipe1Server, pipe1Client = Pipe()
pipe2Server, pipe2Client = Pipe()
def getPipes():
    return pipe1Client, pipe2Client

class MyManager(BaseManager): pass
MyManager.register('getPipes', callable=getPipes)
m = MyManager(address=('', 50000), authkey='A')
s = m.get_server()
t = Thread(target=s.serve_forever)
t.daemon = True
t.start()

print "1", pipe1Server.recv()
print "2", pipe2Server.recv() 

并使用此客户端,您应该能够获取管道并发送给他们

from multiprocessing.managers import BaseManager
class MyManager(BaseManager): pass
MyManager.register('getPipes')
manager = MyManager(address=('127.0.0.1', 50000), authkey='A')
manager.connect()
data = manager.getPipes()
pipe1, pipe2 = data._getvalue()
pipe1.send("spam")
pipe2.send("eggs")

但是我收到此错误:(在客户端进程中)

Traceback (most recent call last):
  File "f.py", line 7, in <module>
    pipe1, pipe2 = data._getvalue()
  File "/usr/lib/python2.7/multiprocessing/managers.py", line 779, in _getvalue
    return self._callmethod('#GETVALUE')
  File "/usr/lib/python2.7/multiprocessing/managers.py", line 759, in _callmethod
    kind, result = conn.recv()
TypeError: Required argument 'handle' (pos 1) not found

现在我认为这与Connection对象被腌制的方式有关但我不知道如何解决这个问题?

0 个答案:

没有答案