[uWSGI] Incompatibility between PyZMQ and uWSGI

Roberto De Ioris roberto at unbit.it
Thu Nov 25 14:01:01 CET 2010


Il giorno 25/nov/2010, alle ore 13.06, Jaime Fernández ha scritto:

> Finally, I found I identified the problem but I don't know why.
> 
> According to ZMQ specification, the ZMQ context should be shared to create all the ZMQ sockets of the same process. What we did, is to create a Python module which creates such ZMQ context:
> 
> # zmq_context.py
> import zmq
> context = zmq.Context()
> 
> If you create the following WSGI app:
> 
> import zmq
> import zmq_context
> def application(environ, start_response):
>     context = zmq_context.context
>     socket_push = context.socket(zmq.PUSH)
>     socket_push.connect('tcp://localhost:9876')
>     socket_push.send_multipart(["Hello", "World"])
> 
> Then uWSGi dies when zmq tries to connect to the ZQM socket.
> 
> However, if we don't use the zmq_context module and create the context directly:
> 
> import zmq
> def application(environ, start_response):
>     context = zmq.Context()
>     socket_push = context.socket(zmq.PUSH)
>     socket_push.connect('tcp://localhost:9876')
>     socket_push.send_multipart(["Hello", "World"])
> 
> Everything works.
> 
> Why? I cannot see any difference. And, as I said, it works with wsgiref (I haven't tested with other WSGi servers)


If you put the initialization out of the application callable, multiple worker processes will use the same socket as
the fork() is called after module loading.

If you use a single uWSGI worker (without master) it should work normally.

The right approach is applying the post_fork_hook patch (you will find it in the list archive, or you can use the current mercurial tip
that already includes it) and define a function to be called after every fork (so you will put context initialization in it)



--
Roberto De Ioris
http://unbit.it



More information about the uWSGI mailing list