ZeroMQ with Python, quick getting started guide

Posted on Thursday, June 28, 2012



Quick ZeroMQ with Python – notes 001
Created 06/28/2012
T. Patrick Bailey



I am currently trying to wrap my head around what ZeroMQ is and what I can do with it.  This is a quick tutorial I wrote on getting it working with Python.
I was inspired by this video showing zeroMQ working in a demo ~ 3:12 into the video http://www.youtube.com/watch?v=_JCBphyciAs&feature=related  [1]

First we need to download ZeroMQ for python and get it working…  I struggled with this for a while but finally got it working.  I am using Ubuntu 10.04 as my OS, here are the steps I took.  I used the following sites to come up with this install procedure https://gist.github.com/574656 [2], http://johanharjono.com/archives/633 [3] http://www.hacksparrow.com/ubuntu-how-to-install-easy_install.html [5]


Install server tools




       > sudo apt-get update
       >    sudo apt-get upgrade
       >    sudo apt-get install build-essential
       >    sudo apt-get install zlib1g-dev
       >    sudo apt-get install libtool autoconf automake
       >    sudo apt-get install uuid-dev g++



Get the web address for the latest zeromq download

http://www.zeromq.org/intro:get-the-software  and right click on the POSIX tarball and copy the address to be used in the following commands with wget



       >  cd /tmp
       >  tar -zvxf zeromq-2.2.0.tar.gz
       >  cd zeromq-2.2.0
       >  ./configure
       >  make
       >  sudo make install



If you are using Ubuntu 12.04 you can do this much easier following these instructions http://ridingpython.blogspot.com/2012/05/installing-zeromq-python-lib-pyzmq-on.html [4]


Get python tools for zeroMQ



       >  sudo apt-get install python-software-properties
       >  sudo add-apt-repository ppa:chris-lea/zeromq
       >  sudo add-apt-repository ppa:chris-lea/libpgm
       >  sudo apt-get update
       >  sudo apt-get install libzmq1 libzmq-dev
       >  sudo apt-get install python-dev
       >  sudo apt-get install python-setuptools
       >  sudo easy_install pyzmq



That should do it, now for a simple test with a  few python scripts.

I am new to ZeroMQ and to python so bear with me on my examples.

First make a zmq socket for sending.  



server-main:~> python
Python 2.6.5 (r265:79063, Apr 16 2010, 13:57:41)
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import zmq
>>> ctx = zmq.Context()
>>> sock = ctx.socket(zmq.DOWNSTREAM)
>>> sock.bind("tcp://127.0.0.1:12345")



This will bind it to the 127.0.0.1 port 12345 and is ready to send



Now in another terminal (on the same machine) make something to receive



server-main:~> python
Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56)
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import zmq
>>> ctx = zmq.Context()
>>> sock = ctx.socket(zmq.UPSTREAM)
>>> sock.connect("tcp://127.0.0.1:12345")
>>> while True:
...   print sock.recv()
...



This will bind on the same port and wait to receive in a loop.  I had to hit enter twice after I typed in the recv line (I am new to python scripting and did not catch that for a while)



Now back to the sending program execute the following line




>>> sock.send(“Hello World”)





And in the receiver we see…








We get the “Hello World” Out.

I sent several more as a test




References
 [1]  Why ZeroMQ?
       By: kfsone
       Visited 06/2012
[2]  ZeroMQ on Ubuntu 10.04 LTS and Rackspace Cloud #
       Visited 06/2012
[3]  Installing ZeroMQ on Ubunut Lucid 10.04
       By Johan Harjono
       Visited 06/2012
[4]  Installing ZeroMQ Python lib pyzmq on Ubuntu 12.04 LTS
       Visited 06/2012
[5]  Ubuntu: how to install easy_install
       Visited 06/2012


2 comments:

  1. Thank you for saving me a lot of time and headache. It is greatly appreciated!

    ReplyDelete