连接到多处理BaseManager在本地工作,但不适合远程服务器

发布于 2025-02-04 14:00:40 字数 2040 浏览 4 评论 0原文

我目前正在使用“ nofollow noreferrer”> supertrocessing.managers.basemanager.basemanager.basemanager.basemanager 服务功能一些Python对象,以便我可以在客户端与他们的代理进行互动。

启动由BaseManager定义的服务器后,我尝试在客户端连接以访问Serveables。但是,仅当服务器和客户端存在在不同的主机上时,我才会看到eoferror。如果他们在同一本地主机上,我可以访问良好。

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)
m = MyManager(address=("127.0.0.1", 60000), authkey=password)
m.start()
# ... long running process ...

client.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

trackback:

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'm currently using the multiprocessing.managers.BaseManager functionality to serve some Python objects so that I could interact with their proxies on the client side.

After I start the server defined by the BaseManager, I try to connect at the client side to access the serveables. However, I'm seeing an EoFError only when the server and client exist on different hosts. If they were on the same local host, I can get access just fine.

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)
m = MyManager(address=("127.0.0.1", 60000), authkey=password)
m.start()
# ... long running process ...

client.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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

っ〆星空下的拥抱 2025-02-11 14:00:40

我只是遇到了同样的问题,但是幸运的是,在调试测试代码的过程中修改代码时,我创建了一个线索,

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()

这导致“在地址上服务...”行打印两次。这使我认为代码被催生了两次。我记得在某个地方看到服务器代码应包含在中,如果__name __ == ... block,所以我尝试了一下,事情开始工作。
我不是专家,但是我认为此错误是在产卵/分叉/等期间引起的。过程。长话短说,您可以像这样修改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()

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.
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.

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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文