在多线程应用程序中,我从一个线程调用rpyc.Connection.serve_all(),其他线程无法立即使用该连接。
我认为serve_all阻塞了连接,其他线程只有在超时时才能访问它。
此代码应会重现该问题
服务器:
#!/usr/bin/env python3
import rpyc
import rpyc.utils.server
import threading
class Service(rpyc.Service):
def on_connect(self):
print("New connection")
def on_disconnect(self):
print("Connection closed")
def exposed_get_status(self):
return "Test string"
server = rpyc.utils.server.ThreadedServer(Service, port = 12345)
t = threading.Thread(target = server.start)
t.daemon = True
t.start()
t.join()客户端:
#!/usr/bin/env python3
import rpyc
import threading
import datetime
con = rpyc.connect('localhost',12345)
def delayed():
print("Time is up")
now = datetime.datetime.now()
print(con.root.get_status())
print(str(datetime.datetime.now() - now))
timer = threading.Timer(10,delayed)
print("Starting timer")
timer.start()
print("serving all")
con.serve_all()示例输出(来自客户端):
$ python3 testrpyc.py
Starting timer
serving all
Time is up
Test string
0:01:30.064087我在安装了pip的Python3.5.4rc1 (debian sid)上使用RPyC 3.4.3。
我想我误用了serve_all,但我在文档中找不到任何东西。
发布于 2017-08-01 20:30:47
(回答我自己)
我在github上打开了一个issue,看起来这很正常。解决方案是对任何资源仅在单个线程上执行IO。
https://stackoverflow.com/questions/45404189
复制相似问题