Google

<previous | contents | next> Pyro Manual

8. Example

In this chapter you'll find a short but complete example that shows how Pyro must be used and how it works. However, much more interesting examples can be found in the examples directory. For the real impatient people, I recommend the "quickstart" example, because you'll see that you can eliminate very much of the (already little!) extra work you have to do to make a Pyro application.

For the really impatient, first two minimalist Pyro examples.

Minimalist's Pyro - not using a Name Server

Server:
import Pyro.core

class JokeGen(Pyro.core.ObjBase):
	def __init__(self):
		Pyro.core.ObjBase.__init__(self)
	def joke(self, name):
		return "Sorry "+name+", I don't know any jokes."

Pyro.core.initServer()
daemon=Pyro.core.Daemon()
uri=daemon.connect(JokeGen(),"jokegen")

print "The daemon runs on port:",daemon.port
print "The object's uri is:",uri

daemon.requestLoop()
Client:
import Pyro.core
import sys

Pyro.core.initClient()

# you have to change the URI below to match your own host/port.
jokes = Pyro.core.getProxyForURI("PYROLOC://localhost:7767/jokegen")

print jokes.joke("Irmen")

Minimalist's Pyro - using a Name Server

Server:
import Pyro.core
import Pyro.naming

class JokeGen(Pyro.core.ObjBase):
	def __init__(self):
		Pyro.core.ObjBase.__init__(self)
	def joke(self, name):
		return "Sorry "+name+", I don't know any jokes."

Pyro.core.initServer()
ns=Pyro.naming.NameServerLocator().getNS()
daemon=Pyro.core.Daemon()
daemon.useNameServer(ns)
uri=daemon.connect(JokeGen(),"jokegen")
daemon.requestLoop()
Client:
import Pyro.core

Pyro.core.initClient()

# finds object automatically if you're running the Name Server.
jokes = Pyro.core.getProxyForURI("PYRONAME://jokegen")

print jokes.joke("Irmen")

There we go with the complete example:

  1. Write a module 'testmod.py' containing a class 'testclass', which will be accessed remotely.
    class testclass:
        def mul(s, arg1, arg2): return arg1*arg2
        def add(s, arg1, arg2): return arg1+arg2
        def sub(s, arg1, arg2): return arg1-arg2
        def div(s, arg1, arg2): return arg1/arg2
  2. Write a server, testserver.py, that creates one or more instances of the 'testclass', and registers them with the Pyro Name Server.
    import sys, socket
    import Pyro.naming
    import Pyro.core
    from Pyro.errors import PyroError,NamingError
    
    import testmod
    
    ###### testclass Pyro object
    
    class testclass(Pyro.core.ObjBase, testmod.testclass):
    	pass
    
    ###### main server program
    
    def main():
    	Pyro.core.initServer()
    	daemon = Pyro.core.Daemon()
    	# locate the NS
    	locator = Pyro.naming.NameServerLocator()
    	print 'searching for Name Server...'
    	ns = locator.getNS()
    	daemon.useNameServer(ns)
    
    	# connect a new object implementation (first unregister previous one)
    	try:
    		ns.unregister('test')		# this is the name by which our object will be known to the outside world
    	except NamingError:
    		pass
    
    	# connect new object implementation
    	daemon.connect(testclass(),'test')
    
    	# enter the server loop.
    	print 'Server object "test" ready.'
    	daemon.requestLoop()
    
    if __name__=="__main__":
    	main()
  3. To make it interesting, the shortest client possible (requires Pyro 2.5 or newer) looks someting like:
    import Pyro.core
    Pyro.core.initClient()
    o=Pyro.core.getProxyForURI('PYRONAME://:Default.test')
    print o.mul(5,33)
    ... But for educational purposes, we use the long way around. Read on.
  4. Write a client, testclient.py, that will find the Name Server.
    import sys, socket
    import Pyro.naming, Pyro.core
    from Pyro.errors import NamingError
    
    Pyro.core.initClient()
    
    # locate the NS
    locator = Pyro.naming.NameServerLocator()
    print 'Searching Name Server...',
    ns = locator.getNS()
    (... continued ...)

  5. Let the client query the NS for the object's URI. Then create a proxy for the remote object. Because the proxy appears the same as the real 'testclass', the client can now invoke methods on the remote objects.

    (...continued from above...)

    # resolve the Pyro object
    print 'finding object'
    try:
    	URI=ns.resolve('test')
    	print 'URI:',URI
    except NamingError,x
    	print 'Couldn\'t find object, nameserver says:',x
    	raise SystemExit
    
    # create a proxy for the Pyro object, and return that
    test = Pyro.core.getProxyForURI(URI)
    
    print test.mul(111,9)
    print test.add(100,222)
    print test.sub(222,100)
    print test.div(2.0,9.0)
    print test.mul('*',10)
    print test.add('String1','String2')
  6. Run the application in the network. First, start the Name Server on one computer.
    irmen@atlantis:~ > ns
    *** Pyro Name Server ***
    Pyro Server Initialized. Using Pyro V2.4
    Will accept shutdown requests.
    URI written to: /home/irmen/Pyro_NS_URI
    URI is: PYRO://10.0.0.150:9090/0a000096-08c30adb-eeb74c26-31095a7a
    Name Server started.
  7. Start the server on another computer (or in a different shell).
    irmen@atlantis:~/ex > python testserver.py 
    Pyro Server Initialized. Using Pyro V2.4
    searching for Name Server...
    Server object "test" ready.
  8. Finally, run the client on a third computer (or in a different shell).
    irmen@atlantis:~/ex > python testclient.py
    *** Using static testmod_proxy.
    Pyro Client Initialized. Using Pyro V2.4
    finding object
    URI: PYRO://10.0.0.150:7766/0a000096-08f20adc-5b774c37-97f7f18a
    999
    322
    122
    0.222222222222
    **********
    String1String2
  9. You might want to peek in the Name Server:
    irmen@atlantis:~/ex > nsc listall
    Finding NS using broadcast @ port 9091
    LOCATOR: Searching Pyro Name Server...
    NS is at 10.0.0.150 (atlantis.lan) port 9090
    -------------- START DATABASE
    :Default.test  -->  PYRO://10.0.0.150:7766/0a000096-08f20adc-5b774c37-97f7f18a
    :Pyro.NameServer  -->  PYRO://10.0.0.150:9090/0a000096-08c30adb-eeb74c26-31095a7a
    -------------- END
  10. And if you're interested you may want to try the logging facility of Pyro. First, set the tracelevel to something other than the default, 0. See the chapter on configuration how to do that. Usually you'll set the environment variable PYRO_TRACELEVEL to 3 (=maximum logging). Then, when you start Pyro programs (like the nameserver), they will write something like this to the logfile:
    ------------------------------------------------------------ NEW SESSION
    2002-01-16 17:11:42   Pyro Initializing, version 2.4
    This is initServer.
    Configuration settings are as follows:
    PYRO_BC_RETRIES = 2
    PYRO_BC_TIMEOUT = 2
    PYRO_BINARY_PICKLE = 1
    PYRO_COMPRESSION = 0
    PYRO_CONFIG_FILE = 
    PYRO_DNS_URI = 0
    PYRO_LOGFILE = /home/irmen/Pyro_log
    PYRO_MAXCONNECTIONS = 200
    PYRO_MOBILE_CODE = 0
    PYRO_MULTITHREADED = 1
    PYRO_NS_BC_PORT = 9091
    PYRO_NS_DEFAULTGROUP = :Default
    PYRO_NS_HOSTNAME = None
    PYRO_NS_PORT = 9090
    PYRO_NS_URIFILE = /home/irmen/Pyro_NS_URI
    PYRO_PORT = 7766
    PYRO_PORT_RANGE = 100
    PYRO_STORAGE = /home/irmen
    PYRO_TRACELEVEL = 3
    PYRO_USER_LOGFILE = /home/irmen/Pyro_userlog
    PYRO_USER_TRACELEVEL = 0
    Init done.
    ----------------------------------------------------------------------
    2002-01-16 17:11:42 ** NOTE ** PYROAdapter ** adapter daemon set to <Pyro Daemon on atlantis:9090>
    2002-01-16 17:11:42 ** NOTE ** NameServer ** created group :Pyro
    2002-01-16 17:11:42 ** NOTE ** NameServer ** created group :Default
    2002-01-16 17:11:42 ** NOTE ** NameServer ** registered NameServer with URI PYRO://10.0.0.150:9090/0a000096-09080add-0c699119-2620c341
    2002-01-16 17:11:42 ** NOTE ** NameServer ** URI written to /home/irmen/Pyro_NS_URI
    2002-01-16 17:11:42 ** NOTE ** NS daemon ** This is Pyro Name Server V2.1.
    2002-01-16 17:11:42 ** NOTE ** NS daemon ** Starting on atlantis port 9090  broadcast server on port 9091

<previous | contents | next> Pyro Manual