Pyro, two way remoting.

Syver Enstad syver-en+usenet at online.no
Wed Dec 26 06:57:51 EST 2001
Hi, I've been using some Christmas time at looking at object remoting.

I have spent most of the time looking at Pyro, which seems to date,
very friendly to get along with indeed. Judging by examples and documentation
Pyro is optimized for a simple client server relationship were the
client access the servers object remotely, and the objects passed to
the server from the client are copied instead of remoted. This is cool
but I wanted to see how/if I could also get the server to remote calls
to the client.

The solution I came up with was the following:

--- begin pyrtest.py ----
import remote
import Pyro.core
import threading


## The server class look like this:
## class Subject:
##     def register(self, object):
##         self._observer = object

##     def doStuff(self):
##         self._observer.update() 



class Client(Pyro.core.ObjBase):
    def __init__(self):
        Pyro.core.ObjBase.__init__(self)
        self.status = 'Alone and hungry'
    def update(self):
        self.status = 'got update'


#pdb.set_trace()
cl = Client()
daemon = Pyro.core.Daemon()
daemon.connect(cl)

class DaemonThread(threading.Thread):
    def run(self):
        daemon.handleRequests()

threadedDaemon = DaemonThread()
threadedDaemon.start()


assert cl.status == 'Alone and hungry', cl.status
subj = remote.get_remote_object('Subject')
subj.register(cl.getProxy())				 
subj.doStuff() # this method calls update on the object passed to register
print 'doStuff returned'
assert cl.status == 'got update', cl.status
print 'the update was okay'

-- end  pyrtest.py --

Some clarifications: If you pass the client object to the *register*
method of *subj* without setting it up as a proxy by inheriting from
*Pyro.core.ObjBase* and calling the *getProxy* method, the last assert
statement will trigger because the client object has been copied over
to the Pyro server and executed there instead of being remoted back to
the client which is what happens in my example.

Is this the "way" to do it? Any suggestion, criticism is very much
appreciated.

PS: The remote module that is used, is provided with Pyro but is not
by default put in a location so that it is accessible for importing
(it's in the Pyro/examples/quickstart directory). The remote module
just takes care of some Pyro boilerplate code for setting up and
accessing a Pyro server.


-- 

Vennlig hilsen 

Syver Enstad



More information about the Python-list mailing list