[zeromq-dev] zeromq & pylons

Nicholas Piël nicholas at nichol.as
Tue Aug 3 22:37:50 CEST 2010

Hi Eric,

On Aug 3, 2010, at 9:43 PM, Eric Bell wrote:

> I am new to both pylons and zeromq, so I have a lot of murkiness about how
> all this works. I imagine that pylons "handlers" come from some sort of
> thread pool, but honestly I have no real idea what's happening under the
> hood.

The only thing what Pylons does (and most other Python web frameworks) is that it provides a WSGI application to some kind of server. 

How your application is actually served depends on the type of webserver you use. The most common (and also Pylons default) are threaded but you could just as well run it on an evented server such as fe Gevent or Tornado. 

> In my initial design, I create zeromq sockets and store them in the
> app_global object. My thinking was that then any handler "thread" could have
> access to the sockets. Upon reading the quote above I am now concerned that
> I have violated a basic restriction on how to use ZeroMQ sockets.

Pylons' app_global is not only shared between different threads but it is also not thread safe and should really only be used for read operations.

The best way to achieve what you are doing is to store the socket context on thread local storage. You can do that by putting something like this in your module file.

    import threading
    lts = threading.local()
    lts.ctx = zmq.Context()

You can then use lts.ctx as a reference to your ZeroMQ context.

Nicholas Piël

More information about the zeromq-dev mailing list