Zero: A Wrapper Around the ØMQ Python Library
A wrapper for a wrapper... Does that make any sense?
This is the question @philipbergen poses in the README for Zero. I can say with almost certainty that it does make sense. I do not use ØMQ but by just looking at some examples of how to use the standard python library and this wrapper, “a wrapper for a wrapper” just makes sense. So let’s look at some code!
Comparing Zero to PyZMQ
Let’s look at the Hello World example client that ØMQ provides in their GitHub repository:
#
# Hello World client in Python
# Connects REQ socket to tcp://localhost:5555
# Sends "Hello" to server, expects "World" back
#
import zmq
context = zmq.Context()
# Socket to talk to server
print "Connecting to hello world server…"
socket = context.socket(zmq.REQ)
socket.connect ("tcp://localhost:5555")
# Do 10 requests, waiting each time for a response
for request in range (10):
print "Sending request ", request,"…"
socket.send ("Hello")
# Get the reply.
message = socket.recv()
print "Received reply ", request, "[", message, "]"
And the Hello World example server that is needed to see it work properly:
#
# Hello World server in Python
# Binds REP socket to tcp://*:5555
# Expects "Hello" from client, replies with "World"
#
import zmq
import time
context = zmq.Context()
socket = context.socket(zmq.REP)
socket.bind("tcp://*:5555")
while True:
# Wait for next request from client
message = socket.recv()
print "Received request: ", message
# Do some 'work'
time.sleep (1) # Do some 'work'
# Send reply back to client
socket.send("World")
Now let’s look at how this would be done in Zero. The equivalent client code would be:
from zero import Zero, ZeroSetup
zero = Zero(ZeroSetup('req', 5555).debugging())
for request in range(10):
reply = zero('Hello')
And the equivalent for the server would be:
from zero import Zero, ZeroSetup
zero = Zero(ZeroSetup('rep', 5555).debugging())
for msg in zero:
zero('World')
(Code samples courtesy of @philipbergen.)
But wait! There's more!
If you call 1-888-CHANGELOG in the next 5 minutes… Oh wait, that’s not right.
Philip has also built a command-line tool around his new library and includes it by default. This way you can even run some of these same examples in your favorite shell. For example,
# Hello World Client
yes Hello | head -10 | zero req 5555 -
# Hello World Server
yes '"World"' | zero rep 5555 -
Installing Zero
At the moment, Philip does not have Zero on PyPI but you can install it by cloning the repository from GitHub and running ./install.sh
.
Discussion
Sign in or Join to comment or subscribe