我正在尝试使用 Pyro4 在服务器上运行一个函数。看一下代码:===========================客户端================ ======
import Pyro4
def square(x):
return x**2
remoteServer = Pyro4.Proxy('PYRONAME:server')
print(remoteServer.evaluate(square, 4))
============================服务器====================== ====
import Pyro4
class Server(object):
def evaluate(self, func, args):
return func(*args)
def main():
server = Server()
Pyro4.Daemon.serveSimple({server: "server"},ns=True)
if __name__ == '__main__':
main()
=========================命名服务器========================
import Pyro4
Pyro4.naming.startNSloop()
==================================================== =========
并得到一个错误:“ Pyro4.errors.ConnectionClosedError:接收:没有足够的数据”。查看下面的完整堆栈跟踪:
Traceback (most recent call last):
File "C:\Users\Alex\Desktop\2.py", line 9, in <module>
print(remoteServer.evaluate(square, 4))
File "C:\Users\Alex\AppData\Roaming\Python\Python34\site-packages\Pyro4\core.py", line 169, in __call__
return self.__send(self.__name, args, kwargs)
File "C:\Users\Alex\AppData\Roaming\Python\Python34\site-packages\Pyro4\core.py", line 380, in _pyroInvoke
msg = message.Message.recv(self._pyroConnection, [message.MSG_RESULT], hmac_key=self._pyroHmacKey)
File "C:\Users\Alex\AppData\Roaming\Python\Python34\site-packages\Pyro4\message.py", line 161, in recv
msg = cls.from_header(connection.recv(cls.header_size))
File "C:\Users\Alex\AppData\Roaming\Python\Python34\site-packages\Pyro4\socketutil.py", line 460, in recv
return receiveData(self.sock, size)
File "C:\Users\Alex\AppData\Roaming\Python\Python34\site-packages\Pyro4\socketutil.py", line 195, in receiveData
raise err
Pyro4.errors.ConnectionClosedError: receiving: not enough data
我希望服务器执行传递给方法(函数)的函数。
PS:Windows7,Python 3.4.1
我发现了类似的问题(How to send a function to a remote Pyro object),但对我不起作用。尝试使用回调装饰器@pyro4.callback,但即使是官方示例(https://github.com/delmic/Pyro4/tree/master/examples/callback:server2.py,client2.py )也不起作用(使用python 3.4 和 python 2.7) => 仅显示消息:“服务器:对客户端执行回调 1”,但没有任何反应。
谢谢