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!

Figure 3: Calling three Pyro scripts for the name server (top), the receiver (bottom left), and the transmitter (bottom right). The colored square is the "remote" object being drawn by receive.py but controlled by transmit.py.

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

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • DIY Scoreboard

    We look at a broadcast video system network that uses Python code to control a video router and check out another program that creates a scoreboard.

  • Nerf Target Game

    A cool Nerf gun game for a neighborhood party provides a lesson in Python coding with multiple processors.

  • Python generators simulate gambling

    Can 10 heads in a row really occur in a coin toss? Or, can the lucky numbers in the lottery be 1, 2, 3, 4, 5, 6? We investigate the law of large numbers.

  • Csound

    The powerful Csound software provides an impressive set of features for audio production and processing. We walk you through the entire system.

  • Panda3D

    Several free game engines are available for Linux users, but programming with them is often less than intuitive. Panda3D is an easy-to-use engine that is accessible enough for newcomers but still powerful enough for the pros at Disney Studios.

comments powered by Disqus
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters

Support Our Work

Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.

Learn More

News