irmen / Pyro5

Pyro 5 - Python remote objects
https://pyro5.readthedocs.io
MIT License
303 stars 36 forks source link

Is there a more convenient callback? #79

Closed liyanes closed 1 year ago

liyanes commented 1 year ago

I would like to use it more conveniently, like this. But not the complicated Callback class.

Server

from Pyro5 import api

dameon = api.Daemon(host='localhost', port=10202)

@api.expose
class Cl:
    def handle(self,func):
        print("func: ", func)
        return func(1,2,3)

    def say(self,s):
        print("Call say: ", s)
        return s

h = Cl()

dameon.register(h, "h")

dameon.requestLoop()

Client

from Pyro5 import api

def func(a,b,c):
    print("Call func: ", a, b, c)
    return a+b+c

pr = api.Proxy("PYRO:h@localhost:10202")
pr.handle(func)

But what I get is an error.

SerializeError unsupported serialized class: builtins.function

I want to simplify the process of callback.

The python version is 3.11.1 The Pyro5 version is 5.14

irmen commented 1 year ago

It's not possible to transfer a reference to a function to the server and expect that to be called transparently. Try to design your program in another way. Also read https://pyro5.readthedocs.io/en/latest/clientcode.html?highlight=callback#pyro-callbacks and look at the callback example https://github.com/irmen/Pyro5/tree/master/examples/callback