tomerfiliba-org/rpyc

server side call client side callback function not work

cocobase opened this issue · 1 comments

I define a client/server RPC service, from server side, I will call client side's function on timer.
Everything is ok ,but when I invoke async callback, it does not work.

I can see print(data) in function on_tick output right data
But at client side, I can't see any output in funciton on_event

Why?

Environment
  • rpyc newest
  • python 3.6
  • operating Windows11
Minimal example

Server Service

class MyServiceFactory(rpyc.Service, metaclass=MetaBase):

    class exposed_MyTickService(object):   # exposing names is not limited to methods :)

        def __init__(self, code_list, callback):
            print(f"news client with {code_list}  and {str(callback)} and ctx {MyServiceFactory.ctx}")
            self.code_list = code_list
            self.callback = rpyc.async_(callback)   # create an async callback
            # 调用订阅
            self.sub_id = getattr(MyServiceFactory.ctx, "subscribe_whole_quote")(code_list, self.on_tick)
            print(self.sub_id)

        def on_tick(self, data):
            print(data)
            self.callback(data)

        def exposed_stop(self):   # this method has to be exposed too
            print("处理tick订阅取消")
            getattr(MyServiceFactory.ctx, "unsubscribe_quote")(self.sub_id)

Client:

lass MyClient(object):

    def __init__(self):
        self.conn = rpyc.connect("localhost", 18810)
        self.bgsrv = rpyc.BgServingThread(self.conn)
        self.root = self.conn.root
        self.service = self.root.MyTickService(["600036.SH"], self.on_event)  

    def on_event(self, tick_data):
        print(f"tick: {tick_data}")

    def close(self):
        self.service.stop()
        self.bgsrv.stop()
        self.conn.close()


if __name__ == "__main__":

    client = MyClient()
    sleep(20)
    client.close()

Additional Information:
I change code in server side, do not use async call, when I call client side function directory, I got a lot of error message. please see attachment.
屏幕截图 2023-10-25 112248