This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Any plan to suppprt BackgroundTask from Starlette? #79
Comments
see #59 for quite a similar
discussion
Le jeu. 14 mars 2019 à 6:18 PM, oterrier <notifications@github.com> a
écrit :
… 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 ?
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#79>, or mute the thread
<https://github.com/notifications/unsubscribe-auth/ABDZPoJBzMNoPbjTwAonRMJb9YJSe8i7ks5vWoRbgaJpZM4b0ykJ>
.
|
You can already do this since it's built on Starlette - make sure you return JSONResponse() otherwise it won't start the background tasks. Example:
|
Again, thanks for your help here guys @euri10 and @wshayes ! It is now integrated into FastAPI in a (probably) more intuitive way (in version The new docs are here: https://fastapi.tiangolo.com/tutorial/background-tasks/ In short: from fastapi import BackgroundTasks, FastAPI
app = FastAPI()
def write_notification(email: str, message=""):
with open("log.txt", mode="w") as email_file:
content = f"notification for {email}: {message}"
email_file.write(content)
@app.post("/send-notification/{email}")
async def send_notification(email: str, background_tasks: BackgroundTasks):
background_tasks.add_task(write_notification, email, message="some notification")
return {"message": "Notification sent in the background"} |
Wow - that's a lot more intuitive and will avoid the - "it's not working because you didn't return the JSONResponse()" issue from Starlette - thank you! |
Awesome! I'm glad to hear you like the design/interface. |
hi all, one similar question:
Is it possible with fastapi? |
@madkote if your processing is in an async function (let's say, created with 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 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 |
Hi! |
@outofnames I guess I would use celery in this scenario. |
Doesn't using celery only for sending emails seems overkill? |
@outofnames well, otherwise you would need to extend HttpExceptino to be able to run background tasks, which might be difficult. Maybe, additionally, I would ask at starlette project directly. |
@outofnames you can probably also use https://docs.python.org/3/library/asyncio-future.html#asyncio.ensure_future |
@tiangolo thanks for the reply, I ended up adding more features to my project just to justify celery :D |
Assuming the original need was handled, this will be automatically closed now. But feel free to add more comments or create new issues or PRs. |
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
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.
Is there any possibility to add this great feature to Fastai ?
The text was updated successfully, but these errors were encountered: