-
|
Starlette allows to attach a list of background tasks to a response, that will run only once the response has been sent. Is there any possibility to add this great feature to Fastai ? |
Beta Was this translation helpful? Give feedback.
Replies: 14 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>
.
|
Beta Was this translation helpful? Give feedback.
-
|
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: |
Beta Was this translation helpful? Give feedback.
-
|
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"} |
Beta Was this translation helpful? Give feedback.
-
|
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! |
Beta Was this translation helpful? Give feedback.
-
|
Awesome! I'm glad to hear you like the design/interface. |
Beta Was this translation helpful? Give feedback.
-
|
hi all, one similar question:
Is it possible with fastapi? |
Beta Was this translation helpful? Give feedback.
-
|
@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 resultIf 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 resultIf 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 |
Beta Was this translation helpful? Give feedback.
-
|
Hi! |
Beta Was this translation helpful? Give feedback.
-
|
@outofnames I guess I would use celery in this scenario. |
Beta Was this translation helpful? Give feedback.
-
|
Doesn't using celery only for sending emails seems overkill? |
Beta Was this translation helpful? Give feedback.
-
|
@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. |
Beta Was this translation helpful? Give feedback.
-
|
@outofnames you can probably also use https://docs.python.org/3/library/asyncio-future.html#asyncio.ensure_future |
Beta Was this translation helpful? Give feedback.
-
|
@tiangolo thanks for the reply, I ended up adding more features to my project just to justify celery :D |
Beta Was this translation helpful? Give feedback.
-
|
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. |
Beta Was this translation helpful? Give feedback.
Again, thanks for your help here guys @euri10 and @wshayes !
It is now integrated into FastAPI in a (probably) more intuitive way (in version
0.10.0).The new docs are here: https://fastapi.tiangolo.com/tutorial/background-tasks/
In short: