und*_*run 5 python connection multiprocessing disconnect
当使用multiprocessingManager 对象创建服务器并远程连接到该服务器时,客户端需要保持与远程服务器的连接。如果服务器在客户端关闭之前离开,客户端将永远尝试连接到服务器的预期地址。
我在服务器消失后试图退出的客户端代码陷入僵局,因为我的客户端进程永远不会退出。
如果我del在服务器宕机之前我的远程对象和我的客户管理器,进程将正常退出,但是在使用后立即删除我的客户的管理器对象和远程对象不太理想。
这是我能做的最好的吗?是否有另一种(更合适的)方法可以断开与远程管理器对象的连接?在服务器关闭和/或连接丢失后,有没有办法干净地退出客户端?
我知道 socket.setdefaulttimeout 不适用于多处理,但是有没有办法专门为多处理模块设置连接超时?这是我遇到问题的代码:
from multiprocessing.managers import BaseManager
m = BaseManager(address=('my.remote.server.dns', 50000), authkey='mykey')
# this next line hangs forever if my server is not running or gets disconnected
m.connect()
Run Code Online (Sandbox Code Playgroud)
更新这在多处理中被破坏了。连接超时需要发生在套接字级别(为了做到这一点,套接字需要是非阻塞的)但非阻塞套接字会破坏多处理。如果远程服务器不可用,则无法放弃建立连接。
这可以帮助你吗?
#### TEST_JOIN_TIMEOUT
def join_timeout_func():
print '\tchild sleeping'
time.sleep(5.5)
print '\n\tchild terminating'
def test_join_timeout():
p = multiprocessing.Process(target=join_timeout_func)
p.start()
print 'waiting for process to finish'
while 1:
p.join(timeout=1)
if not p.is_alive():
break
print '.',
sys.stdout.flush()
Run Code Online (Sandbox Code Playgroud)
(取自python 16.6页面)
通常,超时是在某个 while 循环中测试的。