is there a way to prevent my FastApi server from stopping as a result of my rabbitmq listening to incoming messages using pika client?

31 Views Asked by At

My FastApi server calls a function called consume_messges(), the consume_messages() is a function that creates a pika connection and startsconsuming for incoming message the function is called automatically when my server starts up, but once the function is called it terminates the fastapi server is there a way to make both of them run at the same time or any alternative to make it work

import pika, asyncio
from pms.listeners.auth.auth_listener import auth_listener


async def consume_messages():
    connection = pika.BlockingConnection(pika.ConnectionParameters(host="localhost"))
    channel = connection.channel()

    channel.queue_declare(queue="auth_to_pms")
    channel.basic_consume(
        queue="auth_to_pms", on_message_callback=auth_listener, auto_ack=True
    )

    print(" [*] Waiting for messages. To exit press CTRL+C")
    channel.start_consuming()
    # connection.process_data_events(time_limit=None)


def get_app():
    app = FastAPI()

    app.include_router(pms_router)

    @app.on_event("startup")
    async def startup():
        await consume_messages()
    return app

i expect bthe fastapi server to run and that the consume_message() runs in the background, instead the function terminates the server

0

There are 0 best solutions below