Getting started with PyPy and Autobahn on the RaspberryPi

, Tobias Oberstein

PyPy is a high-performance implementation of Python and Autobahn is a real-time communication framework for Python. Combined, you can do nifty stuff on the Pi in no time.

Introduction

This short post will get you started with PyPy and Autobahn on the Pi - step-by-step and quickly.

We demonstrate a simple Autobahn based server on the Pi and connect both HTML5 and Python clients doing Publish & Subscribe in real-time.

At the end, you'll be able to run the following yourself:

Running Autobahn/PyPy on the Pi

All code for this is open-source and on GitHub - ready to go.

Here is what we cover:

  • What is PyPy?
  • What is Autobahn?
  • Installing PyPy and Autobahn
  • Run your first real-time server
  • What now?

What is PyPy?

I case you've never heard about PyPy (which would be a pity): PyPy is an implementation of the Python language that works with a tracing JIT under the hood.

A tracing JIT will run your program, identify hot loops (code paths that are executed very often) and compile those Python loops into native machine code.

The end result is higher performance compared to the standard CPython - sometimes by a factor of 10 or more. The cost is longer startup time (the JIT needs to "warmup") and higher memory consumption (depends).

The Python language is often identified with it's standard implementation, CPython, but that is just one implementation. PyPy, Jython and IronPython for example are alternative language implementations that strive to be compatible with the Python language and default run-time more or less, and which provide different characteristics.

What is Autobahn?

Autobahn is an open-source real-time framework for the Web, Mobile and the Internet of Things that is based on WebSocket and WAMP:

WebSocket allows bidirectional real-time messaging on the Web and WAMP adds asynchronous Remote Procedure Calls and Publish & Subscribe on top of WebSocket.

The Autobahn project provides open-source implementations of the WebSocket and WAMP protocols in different languages:

  • AutobahnPython
  • AutobahnJS
  • AutobahnAndroid

WAMP is an open protocol and you are not limited to the implementations offered by the Autobahn project - there is a growing list of alternative implementations for various languages and enviroments.

Installing PyPy and Autobahn

Installing PyPy and Autobahn on the Pi is easy - the only thing we assume is that you've already connected your Pi to your local network and know how to login via SSH from your notebook or PC.

Update the System

In general it's a good idea to first update your Pi software base system and packages. To do that, login to your Pi via SSH (the default username is pi and the default password is raspberry) and run

sudo apt-get update
sudo apt-get upgrade

Notes: This might take a long time (like an hour or so) if you've never updated before. More detailed information (including tips on how to automate updates) can be found in the answers to this question here.

After updates have finished, reboot the Pi:

sudo reboot

Installing PyPy

PyPy can be downloaded precompiled for the Pi from the PyPy project site. The version you want for the Pi running Raspbian (the default Linux distribution for the Pi) is this:

ARM Hardfloat Linux binary (ARMHF/gnueabihf, tar.bz2, Raspbian)

Notes: If you run a different distro than Raspbian (or Ubuntu, for which the PyPy also provides binaries), that may or may not work. You might run into issues, since the PyPy binary assumes specific versions of shared libraries, and assumes these libraries to be located under certain paths.

Here are complete steps to install PyPy:

mkdir $HOME/tarballs
cd $HOME/tarballs
wget https://bitbucket.org/pypy/pypy/downloads/pypy-2.2.1-linux-armhf-raspbian.tar.bz2
cd $HOME
tar xvjf tarballs/pypy-2.2.1-linux-armhf-raspbian.tar.bz2

This will unpack PyPy in the home directory and PyPy is then immediately ready-to-run:

pi@raspberrypi ~ $ ./pypy-2.2.1-linux-armhf-raspbian/bin/pypy -V
Python 2.7.3 (87aa9de10f9c, Nov 24 2013, 20:39:35)
[PyPy 2.2.1 with GCC 4.7.2 20120731 (prerelease)]
pi@raspberrypi ~ $ ./pypy-2.2.1-linux-armhf-raspbian/bin/pypy
Python 2.7.3 (87aa9de10f9c, Nov 24 2013, 20:39:35)
[PyPy 2.2.1 with GCC 4.7.2 20120731 (prerelease)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
And now for something completely different: ``PyPy and CPython: they are mortal
enemies intent on killing each other''
>>>>

Now expand your PATH (and LD_LIBRARY_PATH) so that your new PyPy is found:

echo "export PATH=\${HOME}/pypy-2.2.1-linux-armhf-raspbian/bin:\${PATH}" >> ~/.profile
echo "export LD_LIBRARY_PATH=\${HOME}/pypy-2.2.1-linux-armhf-raspbian/lib:\${LD_LIBRARY_PATH}" >> ~/.profile
source ~/.profile

and check that it is indeed available:

pi@raspberrypi ~ $ which pypy
/home/pi/pypy-2.2.1-linux-armhf-raspbian/bin/pypy

Install Setuptools

wget https://bitbucket.org/pypa/setuptools/raw/bootstrap/ez_setup.py -O - | pypy

Verify that the new easy_install tool (which we will subsequently use to install packages into the PyPy environment) is found:

pi@raspberrypi ~ $ which easy_install
/home/pi/pypy-2.2.1-linux-armhf-raspbian/bin/easy_install

Installing Autobahn

Installing Autobahn is now simply a matter of:

easy_install autobahn

This should automatically install not only AutobahnPython from the Python package index, but also all dependencies (like Twisted).

You may want to do a quick check that Autobahn has indeed installed correctly:

pi@raspberrypi ~ $ pypy
Python 2.7.3 (87aa9de10f9c, Nov 24 2013, 20:39:35)
[PyPy 2.2.1 with GCC 4.7.2 20120731 (prerelease)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
And now for something completely different: ``Every time someone argues with
"Smalltalk has always done X", it is always a good hint that something needs to
be changed fast. - Marcus Denker''
>>>> import autobahn
>>>> autobahn.__version__
'0.6.5'
>>>>

Alright, Autobahn is installed at version 0.6.5 and you are ready to go.

Run your first real-time server

We will run a Publish & Subscribe WAMP server on the Pi that communicates in real-time with JavaScript clients running in the browser.

Here is how you can download the example code:

mkdir -p $HOME/example1
cd $HOME/example1
wget https://raw.github.com/tavendo/AutobahnPython/master/examples/wamp/pubsub/simple/example1/server.py
wget https://raw.github.com/tavendo/AutobahnPython/master/examples/wamp/pubsub/simple/example1/index.html
wget https://raw.github.com/tavendo/AutobahnPython/master/examples/wamp/pubsub/simple/example1/autobahn.min.js

If you don't have wget installed on your Pi, do a sudo apt-get install wget. Also, you might prefer to simply clone the complete AutobahnPython Git repository from GitHub: git clone git@github.com:tavendo/AutobahnPython.git

Now start the server ..

pi@raspberrypi ~/example1 $ pypy server.py
2013-12-05 13:57:12+0100 [-] Log opened.
2013-12-05 13:57:12+0100 [-] Using JSON processor 'json'
2013-12-05 13:57:12+0100 [-] WampServerFactory starting on 9000
2013-12-05 13:57:12+0100 [-] Starting factory <autobahn.wamp.WampServerFactory instance at 0xb641f178>
2013-12-05 13:57:12+0100 [-] WampServerFactory starting
2013-12-05 13:57:12+0100 [-] Site starting on 8080
2013-12-05 13:57:12+0100 [-] Starting factory <twisted.web.server.Site instance at 0xb5db78b0>
...

Note: this will take a couple of seconds to startup. Remember, we are running on PyPy, which is JITting code, and the price for that is increased startup time. However, once PyPy has JITted all the hot code, you will get much higher performance. I guess it's another incarnation of "There is no free lunch.";)

.. and open the URL

http://192.168.1.133:8080

in your browser replacing 192.168.1.133 with the IP address (or hostname) of your Pi. Any recent Firefox, Chrome, IE10+ or Safari will do.

To run the Python client from your PC or notebook:

python client.py ws://192.168.1.133:9000

What now?

The Autobahn Web site has a developer zone dedicated to AutobahnPython with all the infos and links like tutorials, reference documentation and how to get in touch.

And the AutobahnPython GitHub repository has dozens of fully working examples for doing both WebSocket based programs, as well as WAMP clients and servers showing off various features of Autobahn and WAMP.

I'd also like to encourage you to join the Autobahn mailing list (or our IRC channel #autobahn on chat.freenode.net) and introduce yourself and your project. We are curious what you are doing and there to help;)

Start experimenting and prototyping for IoT applications with Crossbar.io and Raspberry Pi!

Loaded with all the software you need to make the board part of WAMP applications!



Learn more

Recent posts

Atom Feed

Search this Site

Stay Informed

Sign up for our newsletter to stay informed of new product releases and features:
Community Chat