简体   繁体   English

带回调的 Pyro4

[英]Pyro4 with callbacks

I have the following class definition in a py file:我在 py 文件中有以下类定义:

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.现在我正在尝试使用 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?我的问题是:Pyro4 甚至可以这样做还是这是序列化程序的限制? 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?如果不是,那么您可以为我推荐 Pyro4 的替代方案吗?

Thanks in advance.提前致谢。

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.如果您将序列化程序设置 Pyro4.config.SERIALIZER 全局变量更改为“dill”,则函数回调也将被处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM