Search code examples
pythoncallbackpyro

Pyro4 with callbacks


I have the following class definition in a py file:

class this_obj(object):

    def __init__(self):
         self._apple = 5.0
         self.observ_apple = []

    def setter(self, value):
        if (self._apple != value):
            self._apple = value
            for callback in self.observ_apple:
                callback(self._apple)

    def getter(self):
        return self._apple

    # apply property
    apple = property(getter, setter)

    # binder functions
    def bind_to_apple(self, callback):
        self.observ_apple.append(callback)

And I have this main code in another file:

import handler_obj

def print_on_change(value):
    print("apple change!!! " + str(value))

if __name__ == "__main__":
    q = handler_obj.this_obj()
    q.bind_to_apple(print_on_change)
    print(q.getter())
    q.setter(30)
    print(q.getter())

If you run this code you can see that it is running. Now I am trying to run the same code with Pyro4. As I was doing this I always run into the following error message:

Pyro4.errors.SerializeError: unsupported serialized class: builtins.function

for the following line:

q.bind_to_apple(print_on_change)

My question would be: Is this even possible with Pyro4 or is this a restriction of the serializer? Can this be solved if I try to use pickle instead of serpent?

If not than is there an alternative to Pyro4 which you can suggest for me for such cases?

Thanks in advance.


Solution

  • I just found a solution for that. If you change the serializer setting the Pyro4.config.SERIALIZER global variable to "dill", then the function callbacks will be handled too.