fastapi-cloud-tasks
fastapi-cloud-tasks copied to clipboard
.delay() shoud be async
The example in README shows how to queue a task with sync .delay()
function, which is not a good idea in async handler.
We have remote IO there: usually low-latency, but no warranty. So .delay()
blocks asyncio loop when called from async route.
README presents examples with async routes calling sync .delay()
call example (e.g. "https://github.com/Adori/fastapi-cloud-tasks#local"). This might be misleading for a less experienced user.
Making .delay(...)
async would allow to call
await make_dinner.delay(...)
or even
task = create_task(make_dinner.delay(...))
# make something else here in the meanwhile
# ...
# ensure the task was enqueued before the request has been finalized
await task
Cloud Tasks library already provides async variant of the client for some time, and it works well: https://cloud.google.com/python/docs/reference/cloudtasks/latest/google.cloud.tasks_v2.services.cloud_tasks.CloudTasksAsyncClient