如何使用Pyro代理对象作为工厂?

时间:2012-03-11 14:18:52

标签: python factory-pattern pyro

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

我扩展了Pyro introductory sample code以大致反映我正在尝试做的事情。服务器端代码如下:

# 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的实例一样,但看起来好像是什么时候{ {1}}方法被调用,在服务器端创建make_obj,立即超出范围,因此被垃圾收集。

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

NewObj

......和客户端:

<__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的引用来解决这个问题,并添加某种类型的清理方法......但是真的有必要吗?我在Pyro中遗漏了哪些东西可以帮助我实现这个目标吗?

(为清晰起见而编辑)

2 个答案:

答案 0 :(得分:1)

我最近遇到this feature并正在使用它。对于使用类似工厂模式的代码来说,这是至关重要的。

Pyro Server

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()
#...

答案 1 :(得分:0)

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

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

from new_obj import NewObj

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