我有以下情况;我的Pyro4项目有一个服务器和一个客户端。服务器包含一个需要在同一个回调对象上调用2个回调的方法。因此,课程Callback
有两种回调方法:Callback()
和SecondCallback()
。这些回调方法的调用之间存在一些延迟。我通过调用time.sleep
来模拟我的示例中的延迟。
我需要在Pyro4(Pyro4.config.COMMTIMEOUT
)上设置超时,因为没有一个,Pyro4守护进程永远不会突破requestLoop
方法。当调用一个回调方法时,这非常有效,但是当你必须调用第二个回调方法时,Pyro4回调守护进程会在调用第一个回调方法+超时后关闭连接。
我尝试将超时设置为更大的数量,但此超时也是requestLoop
方法阻止它处理loopCondition的时间。
下面列出了演示我的问题的示例脚本。您需要在启动Pyro4名称服务器后启动服务器启动它:
python -m Pyro4.naming
python test.py -s
然后在新的cmd窗口中启动客户端:
python test.py
Test.py
import Pyro4, time
from argparse import ArgumentParser
ip = "127.0.0.1"
class Server:
def __init__(self):
pass
def ActionOne(self):
return "Foo"
def ActionTwo(self):
return "Bar"
@Pyro4.oneway
def ActionThree(self, callback):
time.sleep(4)
callback.Callback()
time.sleep(3)
callback.SecondCallback()
class Callback:
def __init__(self):
self.Executed = False
pass
def Callback(self):
print "FooBar"
def SecondCallback(self):
print "raBooF"
self.Executed = True
def loopWhile(condition):
while condition:
time.sleep(.1)
if __name__ == "__main__":
parser = ArgumentParser()
parser.add_argument("--server", "-s", action="store_true")
args = parser.parse_args()
if(args.server):
print "Server"
daemon = Pyro4.core.Daemon(host=ip)
uri = daemon.register(Server())
ns = Pyro4.naming.locateNS(host=ip)
ns.register("server", uri)
daemon.requestLoop()
pass
else:
print "Client"
Pyro4.config.COMMTIMEOUT = .5
ns = Pyro4.naming.locateNS(host=ip)
serverUri = ns.lookup("server")
proxy = Pyro4.core.Proxy(serverUri)
print proxy.ActionOne()
print proxy.ActionTwo()
daemon = Pyro4.core.Daemon(host=ip)
callback = Callback()
daemon.register(callback)
proxy.ActionThree(callback)
daemon.requestLoop(lambda: not callback.Executed)
print "FINISHED"
此脚本的结果:
Server
:
Server
Exception in thread Thread-17:
Traceback (most recent call last):
File "C:\Program Files (x86)\IronPython 2.7\Lib\threading.py", line 552, in _T
hread__bootstrap_inner
self.run()
File "C:\Program Files (x86)\IronPython 2.7\Lib\threading.py", line 505, in ru
n
self.__target(*self.__args, **self.__kwargs)
File "test.py", line 22, in ActionThree
callback.SecondCallback()
File "C:\Program Files (x86)\IronPython 2.7\lib\site-packages\Pyro4\core.py",
line 171, in __call__
return self.__send(self.__name, args, kwargs)
File "C:\Program Files (x86)\IronPython 2.7\lib\site-packages\Pyro4\core.py",
line 410, in _pyroInvoke
msg = message.Message.recv(self._pyroConnection, [message.MSG_RESULT], hmac_
key=self._pyroHmacKey)
File "C:\Program Files (x86)\IronPython 2.7\lib\site-packages\Pyro4\message.py
", line 168, in recv
msg = cls.from_header(connection.recv(cls.header_size))
File "C:\Program Files (x86)\IronPython 2.7\lib\site-packages\Pyro4\socketutil
.py", line 448, in recv
return receiveData(self.sock, size)
File "C:\Program Files (x86)\IronPython 2.7\lib\site-packages\Pyro4\socketutil
.py", line 190, in receiveData
raise ConnectionClosedError("receiving: connection lost: " + str(x))
ConnectionClosedError: receiving: connection lost: [Errno 10022] A request to se
nd or receive data was disallowed because the socket is not connected and (when
sending on a datagram socket using a sendto call) no address was supplied
Client
:
Client
Foo
Bar
FooBar
我的最后一个问题是:当调用第二个回调时,如何在COMMTIMEOUT到期后阻止Pyro4关闭连接?
我希望所有这些信息都清楚明白。
感谢您的帮助。
答案 0 :(得分:0)
供将来参考:
我可以通过调用:
重新启动与回调的连接 callback._pyroReconnect()
在调用第二个回调方法之前