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)
Buy Linux Magazine
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.
News
-
Canonical Releases Ubuntu 24.04
After a brief pause because of the XZ vulnerability, Ubuntu 24.04 is now available for install.
-
Linux Servers Targeted by Akira Ransomware
A group of bad actors who have already extorted $42 million have their sights set on the Linux platform.
-
TUXEDO Computers Unveils Linux Laptop Featuring AMD Ryzen CPU
This latest release is the first laptop to include the new CPU from Ryzen and Linux preinstalled.
-
XZ Gets the All-Clear
The back door xz vulnerability has been officially reverted for Fedora 40 and versions 38 and 39 were never affected.
-
Canonical Collaborates with Qualcomm on New Venture
This new joint effort is geared toward bringing Ubuntu and Ubuntu Core to Qualcomm-powered devices.
-
Kodi 21.0 Open-Source Entertainment Hub Released
After a year of development, the award-winning Kodi cross-platform, media center software is now available with many new additions and improvements.
-
Linux Usage Increases in Two Key Areas
If market share is your thing, you'll be happy to know that Linux is on the rise in two areas that, if they keep climbing, could have serious meaning for Linux's future.
-
Vulnerability Discovered in xz Libraries
An urgent alert for Fedora 40 has been posted and users should pay attention.
-
Canonical Bumps LTS Support to 12 years
If you're worried that your Ubuntu LTS release won't be supported long enough to last, Canonical has a surprise for you in the form of 12 years of security coverage.
-
Fedora 40 Beta Released Soon
With the official release of Fedora 40 coming in April, it's almost time to download the beta and see what's new.