[Python] FastAPI background tasks do not work on Azure function apps

Discussão em 'Python' iniciado por Stack, Fevereiro 12, 2025.

  1. Stack

    Stack Membro Participativo

    I followed the documentation to add background tasks to an Azure Function App with FastAPI:

    async def background_task():
    logging.info("Background task started")
    await asyncio.sleep(5)
    logging.info("Background task finished")


    @app.get("/do_background_task")
    async def do_background_task(background_tasks: BackgroundTasks):
    background_tasks.add_task(background_task)
    return {"message": "Background task started, this response should return immediately."}


    However it does not work as expected and the response is only returned after 5 seconds.

    To reproduce this I forked the Microsoft FastAPI example repo and added the code above. The forked repository with reproduction case can be found here.

    It both fails when running locally and on Azure. Is this a limitation from Azure Functions or do I do something wrong?

    Continue reading...

Compartilhe esta Página