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
-
Endless OS 6 has Arrived
After more than a year since the last update, the latest release of Endless OS is now available for general usage.
-
Fedora Asahi 40 Remix Available for Macs with Apple Silicon
If you've been anticipating KDE's Plasma 6 for your Apple Silicon-powered Mac, then you're in luck.
-
Red Hat Adds New Deployment Option for Enterprise Linux Platforms
Red Hat has re-imagined enterprise Linux for an AI future with Image Mode.
-
OSJH and LPI Release 2024 Open Source Pros Job Survey Results
See what open source professionals look for in a new role.
-
Proton 9.0-1 Released to Improve Gaming with Steam
The latest release of Proton 9 adds several improvements and fixes an issue that has been problematic for Linux users.
-
So Long Neofetch and Thanks for the Info
Today is a day that every Linux user who enjoys bragging about their system(s) will mourn, as Neofetch has come to an end.
-
Ubuntu 24.04 Comes with a “Flaw"
If you're thinking you might want to upgrade from your current Ubuntu release to the latest, there's something you might want to consider before doing so.
-
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.