Google

<previous | contents | next> Pyro Manual

4. Pyro Usage

Introduction

This chapter will show the Pyro development process: how to build a Pyro application. For starters, let's repeat the scenario from the Introduction chapter here, but with some more detail:
  1. You write a Python class that you want to access remotely. Do this as if it were a normal Python class (but see the rules in the Rules and Limitations chapter).
  2. You decide if you want to use static precompiled proxies, or Pyro's dynamic proxy. If you choose the latter, skip the next step.
  3. Using pyroc, the proxy compiler, you generate static client proxy code for your Python class.
  4. Write a server process that performs the following tasks:
    • Create one or more instances of your remote object.
    • Create a Pyro Daemon instance.
    • Find the Name Server.
    • Connect the remote object instances to the Daemon that will register them with the Name Server.
    • Sit in a loop telling the Daemon to handle incoming requests.
  5. Write a client program that does the following:
    • Find the Name Server.
    • Query the NS for the requires objects. You'll get URIs back.
    • Import the static proxy module (created by pyroc) and create static proxies for the obtained URI, or just create a Pyro Dynamic proxy.
    • Call methods on the proxy object as if it were the real thing.
  6. Make sure the Pyro Name Server is running.
  7. Start your server process. If it complains that the names it wants to register already exist, use the nsc tool to unregister them, or restart the NS.
  8. Run the client!
In the following sections each step is explained in more detail.

Pyro script tools

Before using them let us first study the usage of the script tools. Pyro comes with two flavors, Un*x-style shellscripts and Windows/DOS batch files. The Windows-style batch files have the '.bat' extension. When you're using an Amiga, you can use the Un*x-style shellscripts when you've installed the latest 'ExecuteHack'. You might want to make them executable using protect +es #?. If it doesn't work, try executing them as regular python scripts, for instance: python nsc list. All scripts have to be called from a shell command prompt. Most of them accept command line parameters, see below.
genguid   (GUID generator)
- One optional argument; -x
- This is a very simple GUID generator. It uses the internal Pyro GUID generator to print a new GUID. If you use the '-x' argument, it prints the GUID in hexadecimal instead of ASCII-encoded format.

pyroc   (Proxy compiler)
- One argument; the name of the module to process (without .py suffix)
- The proxy compiler generates a module with static client proxies for each class in the processed module. If necessary, server side skeleton code is also generated.

ns, rns   (Name Server)
These scripts are explained in the Name Server chapter.

es   (Event Server)
This script is explained in the Event Server (Pyro Services) chapter.

nsc   (Name Server Control tool)
- Arguments: [-h host] [-p port] [-i identification] command [args...]
- Controls the Pyro Name Server. '-h host' specifies the host where the Name Server should be contacted. '-p port' specifies a non-standard NS broadcast port to contact. With '-i identification' you can supply the authentication passphrase that is used to connect to the Name Server. When it contains spaces, use quotes around it. If the host is specified, it is the direct Pyro port instead. 'command' is one of the following:
  • ping: just check if the NS is up and running.
  • list: prints the contents of a name group. Argument is the group name to list.
  • listall: prints a list of all registered names (expanded).
  • register: register a new name. Arguments are the name and the URI.
  • resolve: search for names. Arguments are the names to search for.
  • remove: remove registered names from the NS. Arguments are the names to remove.
  • creategroup: create a name group. Argument is the group to create.
  • deletegroup: delete a name group and all contents. Argument is the group to delete.
  • shutdown: send the NS a shutdown request, so that a clean shutdown is performed. No arguments.

xnsc   (Graphical NS control tool)
- No arguments
- This is a graphical version of the nsc command-line tool. Currently it needs Tk for the GUI, so you have to have a Tk-enabled Python on your system. It doesn't work on AmigaPython for instance. The GUI is simple and should explain itself. You can enter the hostname in the textbox at the top and press <enter> to contact the NS at that host, or just press the 'Auto Discover' button at the top right. If the NS has been found, the rest of the buttons are enabled. If your Name Server requires an authorization passphrase, you must enter that first in the ID entry box. After that, you can connect to the NS. Once connected, the passphrase is erased in the display for security reasons. You have to type it again if you need to reconnect.

Steps 1, 2 and 3: Writing the remote class

Just create a Python module containing the classes you want to access remotely. There are some restrictions induced by Pyro:
  • The remote class can't have a remote __init__ method. You should use a regular initialization method that you must call explicitly after binding to the remote object. The __init__ method will only be called on the server side when the object is created.
  • The remote class can't have direct attribute access unless you conciously choose to use a special proxy that supports attribute access (before Pyro 1.2 this was impossible). See below. You don't have to use getters and setters for each member variable any more.
  • Don't stick the class in a Python package if you want to use static proxies. pyroc can't yet deal fully with Python packages.
If you keep those in mind, you should be safe. You can use all Python types and parameter lists and exceptions in your code. Pyro will deal with those nicely.

Static or dynamic proxy?

Pyro supports two kinds of client-side proxies: static and dynamic. The most important difference between the two is that dynamic proxies don't require additional code because all logic is embedded in Pyro. Static proxies are defined in a Python module that is generated by pyroc. So you need to distribute these additional source files when you want to use static proxies. So why should you even use a static proxy?
  • A static proxy is (slightly) faster than a dynamic one.
  • It has stricter and faster runtime argument checking. A static proxy exactly mirrors the method signatures of the remote class, so an invalid method call is detected by Python locally. A dynamic proxy has to do a remote invocation only to find out the argument list was invalid.
  • But beware: Python has many subtleties and I'm not sure if the current static proxy code that pyroc generates can deal with all of them. It might turn out that the dynamic proxy accepts things that will fail with the static version. Only time will tell.
  • Note: because of group name logic the Name Server can only easily be accessed by the static proxy that Pyro provides (Pyro.naming.NameServerProxy). Luckily this is no big deal.
  • Note: Pyro 1.2 introduced another kind of Dynamic Proxy: one that allows direct object attribute access. This proxy is a few percent slower than the normal dynamic proxy. By the way, the static proxy (generated by pyroc) also supports direct attribute access.

Step 4: Writing the server

Initialization

You should initialize Pyro before using it in your server program. This is done by calling
   Pyro.core.initServer()
If you provide the argument '0', no banner is printed, otherwise a short message is printed on the standard output. If the tracelevel is not zero, a startup message is written to the log. This message shows the active configuration options.

Create a Pyro Daemon

Your server program must create a Pyro Daemon object, which contains all logic necessary for accepting incoming requests and dispatching them to your objects by invoking their methods. You also have to tell the daemon which Name Server to use. When connecting objects to the daemon (see below) it uses this NS to register those objects for you. This is convenient as you don't have to do it yourself.
   daemon = Pyro.core.Daemon()
   daemon.useNameServer(ns)
You can provide several arguments when creating the Daemon:
protocolthe protocol to use (defaults to "PYRO")
hostthe hostname to bind the server on (defaults to '' - the default host). This may be necessary in the case where your system has more than one hostname/IP address, for instance, when it has multiple network adapters. With this argument you can select the specific hostname to bind the server on.
portthe socket number to use (defaults to the PYRO_PORT configuration item)
norangewhether or not to try a range of sockets (leave this at the default value, 0)
publishhostthe hostname that the daemon will use when publishing URIs, in case of a firewall setup. See the Features chapter. Defaults to the value given to the host parameter.

The second line tells the daemon to use a certain Name Server (ns is a proxy for the NS, see the next paragraph how to get this proxy). It's possible to omit this call but the Daemon will no longer be able to register your objects with the NS. If you didn't register them yourself, it is impossible to find them. The daemon will log a warning if it doesn't know your NS.

If your daemon is no longer referenced, it might be garbage collected (destroyed) by Python. Even if you connected Pyro objects to the daemon. So you have to make sure that you keep a reference to your daemon object at all time. This is recommended anyway because you can then cleanly terminate your Pyro application by calling daemon.shutdown() when it exits. Usually this is not a problem because your program creates a deamon and calls its requestLoop. But a situation might arise where you don't keep a reference to the daemon object, and then things break.

Find the Name Server

You have to get a reference to the Pyro Name Server, which itself is a Pyro object. The easiest way is by using the NS Locator:
   locator = Pyro.naming.NameServerLocator()
   ns = locator.getNS()
ns now contains a reference. There are more advanced ways to get a reference to the NS, please read the chapter about the Name Server to find out about them.

Create object instances

The objects you create in the server that have to be remotely accessible can't be created bare-bones. They have to be decorated with some logic to fool them into thinking it is a regular python program that invokes their methods. This logic is incorporated in a special generic object base class that is part of the Pyro core: Pyro.core.ObjBase. There are three ways to achieve this:
  • Derive a new class from both Pyro.core.ObjBase and your original class. The class body can be a simple 'pass'. If you want to add a custom __init__ method, make sure you call the __init__ method of Pyro.core.ObjBase and of your own class, if it has one.
       class ObjectImpl(Pyro.core.ObjBase, test.MyClass):
          def __init__(self):
             Pyro.core.ObjBase.__init__(self)
             test.MyClass.__init__(self)
          ...
       obj = ObjectImpl()
        ... use obj ...
  • Delegate Pattern. In this pattern you create two objects, and you tell one of them to delegate all calls to the other. Instead of deriving from Pyro.core.ObjBase you just create that object and tell it to use your own object as a delegate, by calling the delegateTo method.
       obj = Pyro.core.ObjBase()
       myobj = MyClass()
       obj.delegateTo(myobj)
        ... use obj ...
  • Direct inheritance: subclass your class directly from Pyro.core.ObjBase. This is the least hassle but you have to change existing code if you want to make classes suitable for Pyro.
       class MyPyroObj(Pyro.core.ObjBase):
          def __init__(self):
             Pyro.core.ObjBase.__init__(self)
             ...obj init here...
           ...
       obj = MyPyroObj()
        ... use obj ...

Connect object instances

Ok, we're going nicely up to this point. We have some objects that even already have gotten a unique ID (that's part of the logic Pyro.core.ObjBase gives us). But Pyro still knows nothing about them. We have to let Pyro know we've created some objects and how they are called. Only then can they be accessed by remote client programs. So let's connect our objects with the Pyro Daemon we've created before (see above):
   daemon.connect(obj,'our_object')
That done, the daemon has registered our object with the NS too (if you told it where to find the NS, see above). The NS will now have an entry in its table that connects the name "our_object" to our specific object.
Note 1: if you don't provide a name, your object is a so-called transient object. The daemon will not register it with the Name Server. This is useful when you create new Pyro objects on the server that are not full-blown objects but rather objects that are only accessible by the code that created them. Have a look at the factory and Bank2 examples if this is not clear.
Note 2: the connect method actually returns the URI that will identify this object. You can ignore this if you don't want to use it immediately without having to consult the name service.
Note 3: see the Name Server chapter for more about this and persistent naming.

In contrast to the simple (flat) name shown above ("our_object"), Pyro's Name Server supports a hierarchical object naming scheme. For more information about this, see the Name Server chapter.

The Daemon handleRequest loop

We're near the end of our server coding effort. The only thing left is the code that sits in a loop and processes incoming requests. Fortunately most of that is handled by a single method in the daemon. For many applications calling daemon.requestLoop() is enough. For finer control, you can give a few arguments to the function:
requestLoop(condition, timeout, others, callback)
All arguments are optional. The default is that requestLoop enters an endless loop waiting and handling Pyro requests. You can specify a condition callable object (for instance, a lambda function) that is evaluated each cycle of the loop to see if the loop should continue (the condition must evaluate to 1). The timeout can be used to adjust the timeout between loop cycles (default=3 seconds). The requestLoop doesn't use the timeout (it only returns when the optional loop condition is no longer true), the timeout is simply passed to the underlying handleRequests call. This is required on some platforms (windows) to cleanly handle break signals like ^C. The others and callbacks can be used to add your own socket or file objects to the request handling loop, and act on them if they trigger. For more details, see the paragraph below.

For those that like to have more control over the request handling loop, there is also handleRequests. Usually your loop will look something like this:

   while continueLoop:
      daemon.handleRequests(3.0)
      ... do something when a timeout occured ...
The timeout value in this example is three seconds. The call to handleRequests returns when the timeout period has passed, or when at least one request was processed. You could use '0' for timeout, but this means the call returns directly if no requests are pending. If you want infinite timeout, use 'None'. You can also provide additional objects the daemon should wait on (multiplexing), to avoid having to split your program into multiple threads. You pass those objects, including a special callback function, as follows:
   daemon.handleRequests(timeout, [obj1,obj2,obj3], callback_func)
The second argument is a list of objects suitable for passing as ins list to the select system call. The last argument is a callback function. This function will be called when one of the objects in your list triggers. The function is called with one argument: the list of ready objects. For more information about this multiplexing issue, see the manual page about the Un*x select system call.

This concludes our server. Full listings can be found in the Example chapter.

Step 5: Writing the client

Initialization

You should initialize Pyro before using it in your client program. This is done by calling
   Pyro.core.initClient()
If you provide the argument '0', no banner is printed, otherwise a short message is printed on the standard output. If the tracelevel is not zero, a startup message is written to the log. This message shows the active configuration options.

Find the Name Server

This part is identical to the way this is done in the server. See above. Let's assume that the variable ns now contains the proxy for the NS.

Find object URIs

There are essentially three ways to find an object URI by name:
  • Query the NS. This is the best way to go. You ask the NS to give you the URI for the object with the right name ("my_object" in this example):
       uri = ns.resolve('my_object')
  • Read it from a special file that was written by the object. This is like what is done in the previous paragraph, about finding the NameServer. Be sure to convert the string you read from the file to a real PyroURI object before you use it. Just pass it to the constructur of PyroURI and you'll be fine.
  • Use the special PYRONAME:// or PYROLOC:// URI strings. The first is a shortcut to the Name Server, the second bypasses the Name Server completely. Read more about them in the chapter on the Name Server, "The special PYRONAME:// and PYROLOC:// URIs".
In contrast to the simple (flat) name shown above ("our_object"), Pyro's Name Server supports a hierarchical object naming scheme. For more information about this, see the Name Server chapter.

Create a proxy

You now have a URI in your posession. But you need an object to call methods on. So you create a proxy object for the URI. You can choose to create a dynamic proxy or a static proxy. In the chapter about Pyro Concepts the difference is explained. For the example below, assume that pyroc has generated the mymodule_proxy.py proxy module.
   obj = Pyro.core.getProxyForURI(uri)     # get a dynamic proxy
   obj = Pyro.core.getAttrProxyForURI(uri) # get a dyn proxy with attribute support
   obj = mymodule_proxy.myobject(uri)      # get a static proxy (also w/ attrib support)

   # if you're sure that the URI is a real PyroURI object, you can do this:
   obj = uri.getProxy()                    # get a dynamic proxy directly from the URI	
   obj = uri.getAttrProxy()                # same, but with attribute support
If you're using attribute proxies, be aware of their limitations (described in the Pyro Concepts chapter, under "Proxy").

Remote method invocations

And now what we've all been waiting for: calling remote methods. This is what's Pyro's all about: there is no difference in calling a remote method or calling a method on a regular (local) Python object. Just go on and write:
   obj.method(arg1, arg2)
   print obj.getName()
   a = obj.answerQuestion('What is the meaning of life?')
   # the following statements only work with a attribute-capable proxy:
   attrib = obj.attrib
   obj.sum = obj.sum+1
or whatever methods your objects provide. The only thing to keep in mind is that you need a proxy object whose methods you call.

This concludes our client. Full listings can be found in the Example chapter. For information on using Pyro's logging/tracing facility, see Runtime control and Logging, below.

Steps 6, 7 and 8: Runtime setup

This part is a no-brainer, really. There may be some extra configuration necessary when you're running Pyro behind a firewall, and want to access it from outside the firewall, or have machines with dynamic IP addresses. Please read the chapter "Features and Guidelines" to find out what you have to do. Otherwise it's simple:

Starting the Name Server

A Pyro system needs at least one running Name Server. So, if it's not already running, start one using the ns utility. See Pyro script tools. After starting it will print some information and then the Name Server sits in a loop waiting for requests:
irmen@atlantis:~ > projects/Pyro/bin/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-08620ada-6697d564-62110a9f
Name Server started.  
The NS writes its URI to a file, as it says. This file can be read by other programs, and this is another -very portable- way to discover the NS. Usually you'll want to use the default mechanism from the NameServerLocator (automatic discovery using broadcasting). This is easier. But if your network doesn't support broadcasting, or the NS can't be reached by a broadcast (because it sits on another subnet, for instance), you have to use another method to reach the NS.

Running the server

Just start the python module as you do normally. Before starting, you may want to set certain environment variables to change some of Pyro's configuration items. After starting, your server will usually sit in a loop waiting for incoming requests (method calls, actually).

Running the client

Just start the python module as you do normally. Before starting, you may want to set certain environment variables to change some of Pyro's configuration items.

Runtime control and Logging

Controlling the Name Server

You might want to control the NS while it's running. For instance, to inspect the current registered names or to remove an old name, or to register a new one by hand. You use the nsc command-line utility or the xnsc graphical tool for this purpose, see Pyro script tools.

Controlling Pyro

Pyro has many configuration items that can be changed also during runtime. You might want to set the tracelevel to 3 during a special function, for instance. See the chapter on Installation and Configuration for more information.

Tracing (logging)

Pyro has two distinct logs: the system log and the user log. The system log is used by Pyro itself. You can use it in your own code too, but generally it's better to use the user log. Also see the chapter on Installation and Configuration for detailed info on how to configure the logging options.
  • System Log
    The system log is implemented by the Pyro.util.Log object, which is an instance of Pyro.util.SystemLogger. System log tracelevel is configured using the PYRO_TRACELEVEL config item, the logfile location is configured using the PYRO_LOGFILE config item.
  • User log
    You should create your own user log object by creating a Pyro.util.UserLogger instance. User log tracelevel is configured using the PYRO_USER_TRACELEVEL config item, the user logfile location is configured using the PYRO_USER_LOGFILE config item.
  • Using the Logger object: logging entries
    The logger class provides four methods:
    • msg(source, *args) - log a simple message (note). source is a string that identifies the source of the log entry, after that, any argument may follow to be printed in the logfile.
    • error(source, *args) - log an error. source is a string that identifies the source of the log entry, after that, any argument may follow to be printed in the logfile.
    • warn(source, *args) - log a warning. source is a string that identifies the source of the log entry, after that, any argument may follow to be printed in the logfile.
    • raw(string) - log a string (unformatted). string is the string to write to the logfile. This logging is done unconditionally, the tracelevel setting has no influence here.
    Logfile entries have the following format:
    "2002-01-16 16:45:02 [5884:MainThread] ** ERR! ** NameServerLocator ** Name Server not responding"
    (a date and timestamp, process ID:thread name, then "NOTE", "WARN" or "ERR!", indicating if this was a simple message, a warning or an error. After that, the source of the log entry - this can be any string but should be meaningful for the developer. After that, the actual log message. All elements are separated by two asterisks). Each log entry is one line in the logfile. Entries written by the raw method can have any format, including multiple lines.

Last Notes

Please be sure to read the chapter on Configuration, the Name Server and the chapter about Pyro's Features and Guidelines.

These chapters contain invaluable information about the more detailed aspects and possibilities of Pyro.

Also have a look at the extensions package Pyro.ext, it contains two modules that provide extremely easy remoting for your programs. Take a look at the "quickstart" and "quickstart-noNS" examples for more details.


<previous | contents | next> Pyro Manual