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.
3 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.

- 1,206
- 3
- 13
- 19
-
2Could you give an example of this? I can store data in `threading.local`, but how should it be used? – Alex May 24 '20 at 12:18
Is the ZMQ socket in your app connect()
-ing, or is it bind()
-ing? If your app is considered the client and it's connecting, then multiple instances should be able to connect without issue. If it's considered the server and it's binding, then yes, you'll have problems... but in your case, it seems like you should consider your Flask app to be more transient, and thus the client, and the other end to be more reliable, and thus the server.
But it's hard to really give any concrete advice without code, there's only so much I can intuit from the little information you've given.

- 13,606
- 2
- 29
- 40
ZeroMQ shall not reuse context across different threads. The same applies to sockets.
If you manage to keep the socket used exclusively by one thread in worker, you might reuse the socket.
Anyway, I would start with creating new context and socket with every request and see, if there is any need to go into complexities of sharing a ZeroMQ connection. Set up ZeroMQ is often rather fast.

- 42,725
- 12
- 101
- 98