我使用 RPyC 连接到一个客户端,并调用了一个带参数对象的服务暴露方法。我想要使用这个对象并对其进行操作,但是这个对象是弱引用的。当我想要访问它的数据时,会出现一个 ReferenceError 的错误,提示对象“弱引用对象已经不存在”。
如何安全地保存一个带有弱引用的对象,避免被垃圾回收?如何将其转换为强引用?
server.py (发送消息)
如何安全地保存一个带有弱引用的对象,避免被垃圾回收?如何将其转换为强引用?
server.py (发送消息)
conn = rpyc.connect(ip,port)
bgsrv = rpyc.BgServingThread(conn)
conn.root.my_remote_method(a, b, c) # a,b,c are integer, strings etc.
time.sleep(0.2)
bgsrv.stop()
conn.close()
client.py(处理数据并将其放入队列中)
class MessageService(Service):
def exposed_my_remote_method(self, a, b, c):
ThreadedClient.queue.put([a,b,c])
other.py(读取队列)
def read_queue(self):
""" Handle all the messages currently in the queue (if any) """
while ThreadedClient.queue.qsize():
try:
msg = ThreadedClient.queue.get(0)
self.read_message(msg)
except Queue.Empty:
pass
def read_message(self, msg):
# do something with the data of a, b, c
res = msg[0] + xy # ReferenceError
conn = rpyc.connect(ip, port, config={"allow_public_attrs": True, "allow_pickle":True})
。 - Paula