fastapi-cloud-tasks icon indicating copy to clipboard operation
fastapi-cloud-tasks copied to clipboard

.delay() shoud be async

Open alex-kowalczyk opened this issue 9 months ago • 1 comments

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

alex-kowalczyk avatar Sep 30 '23 05:09 alex-kowalczyk