Pyro – Networking made simple

One for All

© Lead Image © FernandoCortes, 123RF.com

© Lead Image © FernandoCortes, 123RF.com

Article from Issue 230/2020
Author(s):

Pyro allows multiple hardware devices to interact as if they are all on a local machine by hiding the networking.

Several of my projects have required multiple Raspberry Pis working in tandem to accomplish an ultimate goal, such as driving multiple independent displays or integrating a device with a dedicated controlling computer. Sometimes the setup had unique hardware (e.g., sensors); other times distance made it easier to use a remote system and WiFi rather than a lot of cabling. Although you can choose from many approaches to distributed technology, here, I'll focus on the Python remote objects (Pyro) library.

My most recent project that fell in this category was a set of scoreboards for my church's Vacation Bible School. I integrated four large LCD TVs into the set design (Figure 1) and dedicated a Raspberry Pi to each one. Also, I wanted the ability to update each team's score in real time from a centralized console. To accomplish this, I wrote the code for the scoreboards and their controller in Python and communicated between the different screens with Pyro [1].

Figure 1: The four monitors as seen from the audio console. Also shown are two screens for Nintendo Entertainment Systems (center) and worship center projector screens (top). The computer barely visible in the bottom left corner accesses the web manager.

Each Pi runs a Raspbian Lite distro and a custom Python script. After flashing the SD card with Raspbian, the only special configuration was to connect to the WiFi network and install the Pyro library. Python is installed by default with Raspbian, and the PyGame library, also a default, provided the graphics. PyGame can drive the Pi framebuffer directly, so the graphical desktop isn't needed.

Elements of a Pyro System

To begin, I'll look at the three parts of a Pyro system and the network individually. Usually, all parts run on separate hardware, but that's not a requirement; they will happily coexist on a single computer for testing, or if it best fits your application. Figure 2 shows how a Pyro network is laid out.

Figure 2: A typical Pyro network and the flow of a request through the system.

Network

For Pyro to work, physical devices must be able to talk across the network. As long as everything is on the same router (either wired or WiFi), you should be in good shape. To check for basic connectivity between devices, use ip a and ping (see the "Checking Network Connectivity" box for more details). If everything is running on a single computer, you're also in good shape.

Checking Network Connectivity

First you'll need to know the IP address of the device on which your daemon will run. You might already know this information if you've logged in over SSH, but, if not, open a terminal on the "remote" system and type ip a. Then look through the output for inet followed by an IP address. Note that entry 0 is usually localhost, so you're looking for an address that doesn't start with

127.0 ….

Once you have the IP address, go back to your "controller" computer and open a terminal there. You should be able to type

ping <IP address from above>

and start seeing replies. If not, make sure that both computers are on the same network or router. For larger networks, you might also need to adjust your subnet mask.

A mask of 255.255.255.0 requires the first three numbers of the address to be the same. You can change the mask to 255.255.0.0 and require only the first two numbers to be the same. Make this change, reboot both systems, and try your ping again.

Daemons

In Linux, daemons are processes that run in the background to take care of services like printing, checking email, serving web pages, and myriad other tasks. Pyro uses the term "daemon" to describe its workers. A Pyro daemon provides data (class properties) and things it can do (class methods) on the Pyro network. The Python that you write in each daemon makes the provided task happen. Pseudocode looks something like:

@Pyro4.expose
def ringBell():
  bellPin = 17
  GPIO.out ( bellPin , True )
  time.wait ( 1 )
  GPIO.out ( bellPin , False )
  Return "Bell rang for 1 second"

If this looks familiar, you're right! The code to implement your task works just like any other Python code you might write to accomplish a task. The only difference is the decorator on the first line, which lets Python know that it is a Pyro function accessible to the outside world. More on that later.

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

  • 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.

  • Hidden Meaning: Working with Microformats

    Programs aren’t as smart as humans when it comes to interpreting the meaning of web information. If you want to maximize your search rank, you might want to dress up your HTML documents with microformats and microdata.

comments powered by Disqus

Direct Download

Read full article as PDF:

Price $2.95

News