question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Background Tasks stuck other requests when i use a http middleware

See original GitHub issue

First Check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn’t find it.
  • I searched the FastAPI documentation, with the integrated search.
  • I already searched in Google “How to X in FastAPI” and didn’t find any information.
  • I already read and followed all the tutorial in the docs and didn’t find an answer.
  • I already checked if it is not related to FastAPI but to Pydantic.
  • I already checked if it is not related to FastAPI but to Swagger UI.
  • I already checked if it is not related to FastAPI but to ReDoc.

Commit to Help

  • I commit to help with one of those options 👆

Example Code

import time

from fastapi import FastAPI, BackgroundTasks, Request, Response
from loguru import logger

app = FastAPI()


@app.middleware("http")
async def logger_request(request: Request, call_next) -> Response:
    logger.debug(f"http middleware [{request.url}]")
    response = await call_next(request)
    return response


class Worker:
    def __init__(self, arg):
        self.arg = arg
        logger.debug(f"worker[{arg}] init")

    def work(self):
        for i in range(10):
            logger.debug(f"worker[{self.arg}] work[{i}]")
            time.sleep(1)


@app.get("/")
async def root():
    return {"message": "Hello World"}


@app.get("/work/{worker_id}")
async def work(back_task: BackgroundTasks, worker_id: int):
    worker = Worker(worker_id)
    back_task.add_task(worker.work)

    return {"message": f"work[{worker_id}] created"}


@app.get("/hello/{name}")
async def say_hello(name: str):
    return {"message": f"Hello {name}"}

Description

  • router “/work/{worker_id}” would create a task(a sync task, actually) on backgroud, after calling it, other new requests will stuck until the task finish
  • it just happen when i use a http middleware
  • i knew it is a starlette issue but i could not find a solution yet, anybody tell me how to solve this?

Operating System

Windows

Operating System Details

No response

FastAPI Version

0.74.1

Python Version

Python 3.7.10

Additional Context

No response

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
ryuujo1573commented, Mar 2, 2022

Subscribed, thank you.

2reactions
Kludexcommented, Mar 1, 2022
Read more comments on GitHub >

github_iconTop Results From Across the Web

FastAPI Background task in middleware - Stack Overflow
I found some way to solve your problem, i think you can find better way,but for now you can use like this: import...
Read more >
Solve the drawbacks with FastAPI BackgroundTasks - Johachi
Drawbacks of FastAPI's BackgroundTask and how to solve them.
Read more >
Background tasks occasionally stuck at "submitted"
I can't think of any other reason for background jobs to get stuck as submitted, because tasks only enter the submitted state when...
Read more >
Waiting for your ASP.NET Core app to be ready from an ...
NET Core app to be ready to receive requests from inside an ... Whichever approach you take, you can now execute your background...
Read more >
Handling Intensive Tasks with Laravel - Toptal
I chose to use Laravel for this particular task for the following reasons: ... of a Laravel application; they observe the HTTP request...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found