1

I want to be able to use the RabbitMQ consumer to listen for new messages but the thread is blocking and the application does not start up. What's the best approach to handling the listening handler asynchronously?

@asynccontextmanager
async def lifespan(app: FastAPI):
    client = RabbitMQClient()
    await client.connect()
    queue_to_callback = {"request.queue": handleRequestNotification}
    client.start_consuming(queue_to_callback_dict=queue_to_callback)
    client.channel.start_consuming()
    yield
    await client.close()

In my main.py:

FastAPI(lifespan=lifespan)

My assumption was async def was going to make that handler run asynchronously. What's the approach recommended for this and for it to scale to multiple consuming channels?

Kwaku Biney
  • 327
  • 1
  • 3
  • 11
  • Does [this](https://stackoverflow.com/a/76322910/17865804) answer your question? – Chris Jul 27 '23 at 10:10
  • You might find the following answers helpful as well: [this](https://stackoverflow.com/a/70873984/17865804), [this](https://stackoverflow.com/a/76148361/17865804) and [this](https://stackoverflow.com/a/71517830/17865804) – Chris Jul 27 '23 at 10:11
  • 1
    Thanks for these suggestions :) @Chris – Kwaku Biney Jul 27 '23 at 10:12

0 Answers0