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.

[QUESTION] Any plan to suppprt BackgroundTask from Starlette?

See original GitHub issue

Starlette allows to attach a list of background tasks to a response, that will run only once the response has been sent. If the task is a not a coroutine is it executed on a specific executor to not block the event loop.

    if task.is_coroutine():
        future = asyncio.ensure_future(task())
    else:
        loop = asyncio.get_event_loop()
        future = await loop.run_in_executor(None, task.func)

Is there any possibility to add this great feature to Fastai ?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:13 (7 by maintainers)

github_iconTop GitHub Comments

3reactions
tiangolocommented, May 16, 2019

@madkote if your processing is in an async function (let’s say, created with async def process_something(): ...), you can use an async function for your path operation too, and inside of it, use await process_something().

from starlette.concurrency import run_in_threadpool

@app.get("/items"/)
async def read_items():
    result = await process_something("argument 1", keyword_arg2="keyword argument")
    return result

If it’s a normal function, you can use run_in_threadpool from Starlette, pass your standard function and await it, something like:

from starlette.concurrency import run_in_threadpool

@app.get("/items"/)
async def read_items():
    result = await run_in_threadpool(process_something, "argument 1", keyword_arg2="keyword argument")
    return result

If that doesn’t solve your problem, please create a new issue for it so we can continue the discussion there.


As the original issue should be solved now with the support for BackgroundTasks, I’ll close it now. But feel free to add more comments or create new issues.

2reactions
outofnamescommented, Aug 31, 2019

@tiangolo thanks for the reply, I ended up adding more features to my project just to justify celery 😄

Read more comments on GitHub >

github_iconTop Results From Across the Web

Background Tasks - Starlette
Starlette includes a BackgroundTask class for in-process background tasks. A background task should be attached to a response, and will run only once...
Read more >
How to use background tasks with Starlette when there's no ...
Solved! Starlette Middleware: class BackgroundTaskMiddleware(BaseHTTPMiddleware): async def dispatch( self, request: Request, ...
Read more >
Quick and dirty mock service with Starlette - DEV Community ‍ ‍
The Problem: Run a mocked third party service with a delayed webhook ... feature that I saw in the Starlette documentation: Background Tasks....
Read more >
backgroundTask in SwiftUI cannot compile - Apple Developer
In my case, this doesn't help. Indeed my app is only an iPhone and iPad app, no macOS. Regardless, I tried your code...
Read more >
[Python] FastAPI async endpoint with an async background task
As this is a web endpoint that spawns background tasks, ... indeed points to a problem with our instrumentation, they are going to...
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