-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Add rx.event arg to cancel previous background tasks #6793
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
Changes from 5 commits
b7bbced
8afec8e
3508828
1b7c339
20b88a7
58214a9
262fb6c
2a023c0
92a7790
b419ff6
95fd0ce
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -41,6 +41,7 @@ class QueueShutDown(Exception): # noqa: N818 | |
| """Exception raised when trying to put an item into a shut down queue.""" | ||
|
|
||
|
|
||
| CANCEL_KEY = "_cancel_key" | ||
|
tim-haselhoff marked this conversation as resolved.
Outdated
|
||
| _StreamItemT = TypeVar("_StreamItemT") | ||
|
|
||
|
|
||
|
|
@@ -579,6 +580,16 @@ def _create_event_task( | |
| Returns: | ||
| The created asyncio.Task. | ||
| """ | ||
| handler = registered_handler.handler | ||
| cancel_key: tuple[str, str] | None = None | ||
| if handler.is_background and handler.is_cancel_previous_task: | ||
| cancel_key = (entry.ctx.token, entry.event.name) | ||
| for existing in self._tasks.values(): | ||
| if ( | ||
| getattr(existing, CANCEL_KEY, None) == cancel_key | ||
| and not existing.done() | ||
| ): | ||
| existing.cancel() | ||
|
greptile-apps[bot] marked this conversation as resolved.
Outdated
greptile-apps[bot] marked this conversation as resolved.
Comment on lines
+593
to
+595
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
On Python 3.12+, when two matching Useful? React with 👍 / 👎. |
||
| task = asyncio.create_task( | ||
| self._process_event_queue_entry( | ||
| entry=entry, registered_handler=registered_handler | ||
|
|
@@ -587,6 +598,8 @@ def _create_event_task( | |
| ) | ||
| if sys.version_info < (3, 12): | ||
| task._event_ctx = entry.ctx # pyright: ignore[reportAttributeAccessIssue] | ||
| if cancel_key is not None: | ||
| setattr(task, CANCEL_KEY, cancel_key) | ||
| self._tasks[entry.ctx.txid] = task | ||
| task.add_done_callback(self._finish_task) | ||
| return task | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.