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.

Why middleware waits for all scheduled tasks to finish?

See original GitHub issue

Description

I schedule tasks to event loop like this:

    async def wait_to_finish():
        time.sleep(5)
        print('finished')

    @router.get("/async/background")
    async def get() -> bool:
        asyncio.create_task(wait_to_finish())
        return True

And it works well. True returned as response without waiting wait_to_finish() function to finish. And wait_to_finish() function runs in the event loop and prints “finished”. Everything works well until here.

But when I add a middleware like this:

async def catch_exceptions_middleware(request: Request, call_next):
    try:
        return await call_next(request)
    except Exception as exc:
        logger.exception("Exception")
        return JSONResponse(status_code=getattr(exc, "status_code", 500),
                                    content={"message": f"Unexpected error occured."})

app.middleware('http')(catch_exceptions_middleware)

It waits to finish all tasks I created in the event loop before sending response. Thus it takes 5+ secs. to send response, in this example. Why middlewares force the event loop to finish all the tasks? How can I avoid this ? (Without using background-tasks. There are many reason I don’t use it. And remember, this works pretty well until I add a middleware as shown above).

Environment

  • OS: macOS
  • FastAPI Version : 0.61.0
  • Python version: 3.8.5

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:18 (14 by maintainers)

github_iconTop GitHub Comments

1reaction
Kludexcommented, Sep 25, 2020

That’s pretty disappointing… Thank you @ycd

1reaction
ycdcommented, Sep 24, 2020

@Kludex doesn’t using a Middleware that inherits BaseHTTPMiddleware brokes the BackgroundTasks?

See Starlette Issues 919

Read more comments on GitHub >

github_iconTop Results From Across the Web

using @Scheduled and @Async together? - Stack Overflow
ScheduledThreadPoolExecutor waits until Runnable#run() is finished and sets the next execution time using the start time and the fixed rate.
Read more >
2 Managing Scheduled Tasks - Oracle Help Center
This scheduled task gets back the result of SoD Evaluation from the SoD Server, for example, OAACG, SAP, and GRC for all requests...
Read more >
Scheduled tasks with Hyperlambda and retry - Aista
First of all it's using a semaphore to make sure maximum 8 tasks are executed simultaneously. If task number 9 tries to execute,...
Read more >
Middleware Support for Aperiodic Tasks in ... - CiteSeerX
To fairly compare with the alternative approach (AUB), all periodic tasks and the server itself are scheduled by a preemptive end-to-end deadline monotonic....
Read more >
Background tasks with hosted services in ASP.NET Core
However, tasks aren't abandoned after cancellation is requested—the caller awaits all tasks to complete. If the app shuts down unexpectedly ...
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