Pyro – Networking made simple
Controllers
Now that you have a set of daemons out on the network ready to do your bidding, you need a controller (or two or 10) to direct those tasks. Here is where Pyro starts to shine through. After a few lines of configuration, you call a daemon as if it's a local function. All of the networking, routing, connections, packets, and data transfer happens behind the scenes. The code to ring the bell would be:
message = remoteBell.ringBell()
If the remote function returns any data, it is passed back over the network and returns normally. A Pyro network can have multiple controllers, and each controller can connect to multiple daemons. You can start to see how this arrangement can simplify an otherwise complex system.
Name Server
The name server, similar to its web counterpart, is the "phone operator" of the system. Daemons connect to the nameserver and say something like, "My service is called 'bell' and I can ring a bell." Controllers connect to the name server and ask, "Where can I find a service named 'bell'?" The name server consults its phone book and returns a URI for the bell daemon. After that, the two are free to communicate directly, even if the name server goes offline. Just like a telephone system, if you know the URI of your desired daemon, you can connect to it directly without a name server.
Starter Example
The first example is a complete Pyro system with a Python daemon that draws a colored square on a screen with the PyGame library. The controller asks for user input and then draws the square as requested. The two scripts talk via Pyro and a name server. Everything is set up to run on a single computer, and you should run each script in its own terminal.
Before you begin, install Pyro (see the "Installing Pyro" box for instructions and testing) and open three terminals. In terminal 1, start a name server (Figure 3, top terminal):
python -m Pyro4.naming
Installing Pyro
The Pyro library isn't installed by default, so you need to open a terminal and type
sudo pip3 install Pyro4
to download and install Pyro. You can confirm that it is installed with:
python3 >>> import Pyro4
If you get another Python prompt and no error messages, you are good to go!
The -m
tells Python to run this module as a script. In this case, Pyro will start a name server. The name server doesn't say much, but it is very efficient at its job and lets all of the scripts communicate with each other seamlessly.
In terminal 2, start the receiver (Figure 3, bottom left):
python receive.py
Nothing will happen until the transmitter initiates a connection, so in terminal 3, start the transmitter (Figure 3, bottom right):
python transmit.py
Once the transmitter starts, it initiates the Pyro connection, and the receiver, in this example, draws a square on the screen; then, the transmitter asks for input. It understands the commands RED
, GREEN
, and BLUE
or three numbers (0-255) separated by commas. If Python can split the three numbers successfully, it treats them as an RGB value and sets the square color appropriately (e.g., 200,200,200
colors the box gray). After I've shown you how to set up the code with Pyro later in this article, you can try setting the box color a few times and then use TIME
to see how long you've been playing with the system (Figure 3, bottom right).
Buy this article as PDF
(incl. VAT)