[英]Connecting to Multiprocessing BaseManager works locally but not for remote server
I'm currently using themultiprocessing.managers.BaseManager functionality to serve some Python objects so that I could interact with their proxies on the client side.我目前正在使用multiprocessing.managers.BaseManager功能来提供一些 Python 对象,以便我可以在客户端与它们的代理进行交互。
After I start the server defined by the BaseManager, I try to connect at the client side to access the serveables.启动 BaseManager 定义的服务器后,我尝试在客户端连接以访问可服务对象。 However, I'm seeing an EoFError
only when the server and client exist on different hosts.但是,只有当服务器和客户端存在于不同的主机上时,我才会看到EoFError
。 If they were on the same local host, I can get access just fine.如果他们在同一个本地主机上,我可以很好地访问。
server.py服务器.py
from multiprocessing.managers import BaseManager
password = "password".encode("utf-8")
class MyManager(BaseManager):
pass
a = {'one': 1} # to be served
MyManager.register("get_a", callable=lambda: a)
m = MyManager(address=("127.0.0.1", 60000), authkey=password)
m.start()
# ... long running process ...
client.py客户端.py
from multiprocessing.managers import BaseManager
password = "password".encode("utf-8")
class MyManager(BaseManager):
pass
MyManager.register("get_a")
manager = MyManager(address=("SERVER_IP", 60000), authkey=password)
manager.connect() # raises EoFError
Traceback:追溯:
File "client.py", line 20, in connect_to_executor_manager
manager.connect()
File "/usr/lib/python3.7/multiprocessing/managers.py", line 512, in connect
conn = Client(self._address, authkey=self._authkey)
File "/usr/lib/python3.7/multiprocessing/connection.py", line 498, in Client
answer_challenge(c, authkey)
File "/usr/lib/python3.7/multiprocessing/connection.py", line 741, in answer_challenge
message = connection.recv_bytes(256) # reject large message
File "/usr/lib/python3.7/multiprocessing/connection.py", line 216, in recv_bytes
buf = self._recv_bytes(maxlength)
File "/usr/lib/python3.7/multiprocessing/connection.py", line 407, in _recv_bytes
buf = self._recv(4)
File "/usr/lib/python3.7/multiprocessing/connection.py", line 383, in _recv
raise EOFError
I just ran into the same problem, but luckily, I created a clue when I modified my code in the course of debugging我刚刚遇到了同样的问题,但幸运的是,我在调试过程中修改代码时创造了一个线索
from multiprocessing.managers import BaseManager
password = "password".encode("utf-8")
class MyManager(BaseManager):
pass
a = {'one': 1} # to be served
MyManager.register("get_a", callable=lambda: a)
m = MyManager(address=("127.0.0.1", 60000), authkey=password)
print(f"serving at address {m.address}") # I added the equivalent of this line
m.start()
My test code caused the "serving at address..." line to print twice.我的测试代码导致“在地址服务...”行打印两次。 This made me think that the code was getting spawned twice.这让我觉得代码被生成了两次。 I recall seeing somewhere that the server code should be contained in an if __name__ ==...
block, so I tried that and things started working.我记得在某处看到服务器代码应该包含在if __name__ ==...
块中,所以我尝试了,事情开始工作了。
I'm no expert, but I think this error is caused during the spawn/forking/etc.我不是专家,但我认为这个错误是在产卵/分叉/等过程中引起的。 process.过程。 Long story short, you can modify your server.py
code like so and it will likely work.长话短说,您可以像这样修改您的server.py
代码,它可能会起作用。
from multiprocessing.managers import BaseManager
password = "password".encode("utf-8")
class MyManager(BaseManager):
pass
a = {'one': 1} # to be served
MyManager.register("get_a", callable=lambda: a)
if __name__ == '__main__':
m = MyManager(address=("127.0.0.1", 60000), authkey=password)
m.start()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.