12/21/2023 0 Comments Pika rabbit![]() I changed the example so it creates an AsyncioConnection rather than using SelectConnection, because FastAPI has already started the standard asyncio event loop and I want pika to use that rather than whatever SelectConnection decided to use. I have something working by adapting the pika async publisher example. I'd prefer to do it the 'proper' way and use async code.ĮDIT: There does not seem to be a way to do a blocking read, so and endless loop with channel.basic_get() and sleep instead. My other idea was to still use a thread but have the thread perform a blocking read on the RabbitMQ connection in the hope that will keep sending keepalives over it, and that it won't interfere with publishing over the same connection from the path method. JSONObject = Dict[str, def webhook_endpoint(msg: JSONObject): Is anyone able to show me how, given the trivial FastAPI app below, I can open a RabbitMQ connection that will have the necessary keepalives sent over it to keep it open, such that I can publish via the connection in the path method? from fastapi import FastAPI, Response I cannot understand how to adapt the async examples in either pika or aio-pika map to a FastAPI app. My code catches exceptions on write to RabbitMQ and reconnects and tries again but that's pretty ugly. The problem with this simple-minded approach is that because the 2nd thread is blocking on reading the SimpleQueue the RabbitMQ connection is not getting the keepalives sent over it and the server closes it. So the FastAPI path method writes the object to this SimpleQueue and the 2nd thread reads the object from it and publishes it to RabbitMQ. My current 'solution' is to start a 2nd thread in a FastAPI app startup method, and use a queue.SimpleQueue to talk between the threads. None of the other questions or answers here on SO are helping. I cannot figure out how to connect to RabbitMQ, create the channel, and keep it all alive by being hooked into the FastAPI asyncio loop. I am trying to create a webhook endpoint with FastAPI, and write any json request body that arrives there to RabbitMQ.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |