Replies: 5 comments
-
So, FastAPI is a web framework, you would have clients that start an HTTP (or WebSocket) connection and start communicating at that point. I'm not sure I understand what or how would you want to use ZMQ with FastAPI 🤔 If you need a process listening to ZMQ I would imagine you would start a separate process with its own code to handle it. I can't imagine in which case it would be useful to have it running in the same process as FastAPI... 💭 I'm also not sure what you refer to with:
|
Beta Was this translation helpful? Give feedback.
-
Assuming the original issue was solved, it will be automatically closed now. But feel free to add more comments or create new issues. |
Beta Was this translation helpful? Give feedback.
-
yup. but want to append that using jeff bass awesome imagezmq to work around a pretty resourceheavy mjpeg streaming response is highly recommended. :) |
Beta Was this translation helpful? Give feedback.
-
@ambush276 @haviduck I know I'm late to the party. But please checkout jina. We use both FastAPI & ZMQ extensively! |
Beta Was this translation helpful? Give feedback.
-
Adding to the above.
Our use case is mq-to-ws and http-to-mq bridge where browser talks to the backend (also ZMQ Router), which issues MQ commands to worker nodes (ZMQ Dealers) in response to HTTP requests. On the other side, updates from workers are converted to WebSocket updates (we use AsyncIO mounted mode) and sent back to browser. FastAPI app also manages state of all this in the database, so there's a lot of shared Service/DAO logic that is triggered by both HTTP API calls and MQ commands. One big issue here is how to scale/share single ZMQ socket across multiple Uvicorn workers, we are running separate instance with single worker, but that gets very busy very fast |
Beta Was this translation helpful? Give feedback.
-
I am surprised this has not really been asked in detail but for some reason i could not find this question or solution anywhere. It seems to be that a lot of people are having a problem where you have a fastAPI application that also needs to communicate to some other microservice (in something more efficient than http messages). I have read all of the zmq documentation on integration into asyncio, but so far i have not found anything about how to add zmq into the eventloop with fastapi (or even starlette for that matter). here is an example of the code from the zmq website:
This shows us an async function, which is great, but once again this needs to run in the eventloop alongside the fastAPI coroutine. How is this supposed to be done? The fastAPI documentation does not really provide any interface for us to run a separate coroutine besides a background task. I am not sure if there is any other magic happening in a background task, but for something that needs to communicate with another microservice, i would want this to have scheduling similar to the fastAPI coroutine. Furthermore, you can't start a background task at startup, so you have to do some hokey call to even get it running (it's hacky .. but technically works).
This would maybe allow us to run zmq INSIDE the fastAPI coroutine, just binding another port, and making sure that all traffic from that port goes to this special app.set method. I would be more than fine with something like this ...
This is ideally what i would like, using the fastAPI coroutine... but having zmq sockets invoke responses to a specific function. Is either creating a coroutine example OR this sort templated example possible? If not, how are people claiming you can use zmq with fastapi efficiently? (i guess background tasks could do the trick.. but it seems really hokey)
Beta Was this translation helpful? Give feedback.
All reactions