[zeromq-dev] Python bindings with poll.

Brian Granger ellisonbg at gmail.com
Wed Feb 24 19:26:15 CET 2010


Carlos,

> The poll support follows the interface of select.poll in Python's
> standard library.
>
> The main difference with Brian's bindings
> (http://github.com/ellisonbg/pyzmq/) is that they don't require
> cython.

Just to clarify....Cython is not a dependence for users.  The source
of a Cython extension
module looks similar to pure Python, but has two important differences:

* It has a .pyx file extension rather than .py
* It can call C code directly and declare C variables (fast).  In that
sense a .pyx file
is a linear combination of C and Python.

Here is the .pyx for my bindings:

http://github.com/ellisonbg/pyzmq/blob/master/zmq/_zmq.pyx

When you pass _zmq.pyx through Cython, it autogenerates a C extension, _zmq.c:

http://github.com/ellisonbg/pyzmq/blob/master/zmq/_zmq.c

Note that this extension code is included in the pyzmq source.  For a
regular user,
building this extension is just the same as building a hand-written
extension - Cython
does not have to be installed on the system (if that requirement has
crept in it is a bug).
The only time Cython is required is if a developer wants to modify the
.pyx file.

Why is it advantageous to use Cython, when you can write an extension by hand?

1. Cython is able to generate code for all current Python versions
(2.5, 2,6, 3.x).  Because
the Python C-apis have changed as of 3.x, this can be pretty painful
to do in a hand written
extension.  Because of this feature, many of the core Python
developers are saying that
Cython should become *the* preferred way for most projects to write extensions.

2. Cython allows you to use pure Python syntax when you want to.  Here
is an example
in my code of a send method that encodes a Python object as json:

http://github.com/ellisonbg/pyzmq/blob/master/zmq/_zmq.pyx#L393

Notice how this pure Python sits right along side the C calls.  To do
things like
this with a hand written extension you either i) have to have a pure
python wrapper around your extension
or 2) write that stuff in C :(

3. The overhead of Cython is extremely minimal.  When I measured the latencies
and throughputs of my Cython based extension they were on par with the
hand written
extension.  This is consistent with the other projects I have used Cython for.

4. Maintenance.  Writing extensions by hand is pretty painful and
error prone..."Hmm, do
I need a DECREF here or not,..."  Cython takes care of all of that...

5. Everyone is doing it.  Cython

In summary, obviously, anyone is free to write or maintain different
Python bindings for zeromq,
but I think there are huge advantages to using Cython and I plan on
sticking with it.

Cheers,

Brian



> Comments and feedback welcome.
>
> -- Carlos
> _______________________________________________
> zeromq-dev mailing list
> zeromq-dev at lists.zeromq.org
> http://lists.zeromq.org/mailman/listinfo/zeromq-dev
>



-- 
Brian E. Granger, Ph.D.
Assistant Professor of Physics
Cal Poly State University, San Luis Obispo
bgranger at calpoly.edu
ellisonbg at gmail.com



More information about the zeromq-dev mailing list