-
Notifications
You must be signed in to change notification settings - Fork 55
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
Abort async tasks using asyncio #1190
Conversation
Coverage reportClick to see where and how coverage changed
This report was generated by python-coverage-comment-action |
5932d48
to
229e086
Compare
229e086
to
6ace0f5
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll need to make a more thorough review soon but it seems good :)
docs/howto/advanced/cancellation.md
Outdated
await do_something() | ||
``` | ||
|
||
It is possible to prevent the job from aborting by capturing asyncio.CancelledError. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is possible to prevent the job from aborting by capturing asyncio.CancelledError. | |
It is possible to prevent the job from aborting by [shielding](https://docs.python.org/3/library/asyncio-task.html#shielding-from-cancellation) (and/or with `except asyncio.CancelledError`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Technically, what prevents the job from being aborted is catching the error, not shielding.
In practice, it is likely the combination of both that the user wants.
How would you prefer to word it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you want to have some custom behavior at cancellation time, use a combination of [shielding](https://docs.python.org/3/library/asyncio-task.html#shielding-from-cancellation) and `except asyncio.CancelledError`.
procrastinate/worker.py
Outdated
) | ||
|
||
for task in tasks_to_cancel: | ||
task.cancel() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's worth logging something (in debug) for each cancelled task. I think I would remove the
and context.task
and asyncio.iscoroutinefunction(context.task.func)
above, and instead make a dedicated function that receives a task & context and logs and cancels it if it's a coroutine.
Seems like you know how to make an empty commit to trigger CI. Note; you can combine this with |
Congratulations again :) |
Closes #1084
Successful PR Checklist:
PR label(s):
Changes
This modifies the way jobs are being aborted when the task is async, by cancelling the underlying asyncio Task that represents the running job.