3

我想将 Pyro 与涉及工厂模式的现有类集一起使用,即 A 类的对象(通常只有其中一个)用于实例化 B 类的对象(可以有任意数量的这些) 通过工厂方法。因此,我将 A 类对象公开为 Pyro 代理对象。

我已经扩展了 Pyro介绍性示例代码,以大致反映我正在尝试做的事情。服务器端代码如下:

# saved as greeting.py
import Pyro4
import socket

class NewObj:
    func_count = None
    def __init__(self):
    print "{0} ctor".format(self)
        func_count = 0
    def __del__(self):
    print "{0} dtor".format(self)
    def func(self):
    print "{0} func call {1}".format(self, self.func_count)
    self.func_count += 1

class GreetingMaker(object):
    def __init__(self):
    print "{0} ctor".format(self)
    def __del__(self):
    print "{0} dtor".format(self)
    def get_fortune(self, name):
    print "getting fortune"
        return "Hello, {0}. Here is your fortune message:\n" \
               "Behold the warranty -- the bold print giveth and the fine print taketh away.".format(name)
    def make_obj(self):
    return NewObj()

greeting_maker=GreetingMaker()

daemon=Pyro4.Daemon(host=socket.gethostbyname(socket.gethostname()), port=8080)                                      # make a Pyro daemon
uri=daemon.register(greeting_maker, "foo")  # register the greeting object as a Pyro object

print "Ready. Object uri =", uri            # print the uri so we can use it in the client later
daemon.requestLoop()                        # start the event loop of the server to wait for calls

客户端代码也略有改动:

# saved as client.py
import Pyro4

uri="PYRO:foo@10.2.129.6:8080"
name="foo"

greeting_maker=Pyro4.Proxy(uri)          # get a Pyro proxy to the greeting object
print greeting_maker.get_fortune(name)   # call method normally
print greeting_maker.make_obj()

我的意图是能够创建NewObj和操作它们的实例,就像我可以GreetingMaker在客户端操作实例一样,但看起来好像发生的情况是当make_obj方法被调用时,NewObj在服务器端创建 a,立即下降超出范围,因此被垃圾收集。

这是输出的样子,服务器端:

<__main__.GreetingMaker object at 0x2aed47e01110> ctor
/usr/lib/python2.6/site-packages/Pyro4-4.12-py2.6.egg/Pyro4/core.py:152: UserWarning: HMAC_KEY not set, protocol data may not be secure
  warnings.warn("HMAC_KEY not set, protocol data may not be secure")
Ready. Object uri = PYRO:foo@10.2.129.6:8080
getting fortune
<__main__.NewObj instance at 0x175c8098> ctor
<__main__.NewObj instance at 0x175c8098> dtor

...和客户端:

/usr/local/lib/python2.6/dist-packages/Pyro4-4.12-py2.6.egg/Pyro4/core.py:152: UserWarning: HMAC_KEY not set, protocol data may not be secure
  warnings.warn("HMAC_KEY not set, protocol data may not be secure")
Hello, foo. Here is your fortune message:
Behold the warranty -- the bold print giveth and the fine print taketh away.
Traceback (most recent call last):
  File "client.py", line 9, in <module>
    print greeting_maker.make_obj()
  File "/usr/local/lib/python2.6/dist-packages/Pyro4-4.12-py2.6.egg/Pyro4/core.py", line 146, in __call__
    return self.__send(self.__name, args, kwargs)
  File "/usr/local/lib/python2.6/dist-packages/Pyro4-4.12-py2.6.egg/Pyro4/core.py", line 269, in _pyroInvoke
    data=self._pyroSerializer.deserialize(data, compressed=flags & MessageFactory.FLAGS_COMPRESSED)
  File "/usr/local/lib/python2.6/dist-packages/Pyro4-4.12-py2.6.egg/Pyro4/util.py", line 146, in deserialize
    return self.pickle.loads(data)
AttributeError: 'module' object has no attribute 'NewObj'

我怀疑我可以通过让工厂类(即GreetingMaker)保留对它创建的每一个的引用NewObj,并添加某种清理方法来解决这个问题……但这真的有必要吗?我是否在 Pyro 中遗漏了一些可以帮助我实现这一点的东西?

(为清楚起见进行了编辑)

4

2 回答 2

2

我最近遇到了这个功能并正在使用它。这对于我使用类似工厂模式的代码至关重要。

火焰兵服务器

class Foo(object):
    def __init__(self, x=5):
        self.x = x

class Server(object):
    def build_foo(self, x=5):
        foo = Foo(x)
        # This line will register your foo instance as its own proxy
        self._pyroDaemon.register(foo)
        # Returning foo here returns the proxy, not the actual foo
        return foo

#...
uri = daemon.register(Server()) # In the later versions, just use Server, not Server()
#...
于 2015-12-01T06:41:35.473 回答
0

这里的问题是在服务器端pyro腌制NewObj对象,但它无法在客户端取消腌制它,因为客户端NewObj不知道实现。

解决问题的一种方法是创建第三个模块,例如new_obj.py,然后将其导入服务器和客户端,如下所示:

from new_obj import NewObj

这将让客户端解开NewObj实例并使用它。无论如何,请注意它将是存在NewObj于客户端中的真实对象,而不是存在于服务器中的对象的代理。

于 2012-03-11T18:14:42.713 回答