Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the appropriate way to use Flask with zeromq in production?

I have a Flask app that accepts HTTP requests. When certain HTTP requests come in, I want to trigger a message on a zeromq stream. I'd like to keep the zeromq stream open all the time. I'm wondering what the appropriate way to do this is. Since it is recommended to use gunicorn with Flask in production, doesn't that mean that there will be multiple instances of the Flask app, and if I put the zeromq connection in the same place as the Flask app, only one of those will be able to connect, and the others will fail.

like image 277
repalviglator Avatar asked Jul 18 '14 21:07

repalviglator


People also ask

What is ZeroMQ used for?

ZeroMQ provides a whole slew of language APIs which run on most operating systems and allows you to communicate seamlessly between all sorts of programs. It also provides a collection of patterns, such as request-reply and publish-subscribe which assist you in creating and structuring your network.

Is ZeroMQ TCP or UDP?

ZeroMQ sockets carry messages, like UDP, rather than a stream of bytes as TCP does. A ZeroMQ message is length-specified binary data.

Is ZeroMQ persistent?

The ZeroMQ Guide has a persistence pattern called Titanic. It's based on a pattern called MajorDomo.

What is ZeroMQ context?

A ØMQ context is thread safe and may be shared among as many application threads as necessary, without any additional locking required on the part of the caller. Individual ØMQ sockets are not thread safe except in the case where full memory barriers are issued when migrating a socket from one thread to another.


1 Answers

I use a threading.local() object to store the zeromq context and socket objects.

That way I can re-use the already connected sockets inside a thread, while ensuring each thread will have its own socket objects.

like image 172
Philippe Avatar answered Oct 05 '22 11:10

Philippe