-
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
base: main
Are you sure you want to change the base?
Changes from all 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 |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Added argument to rx.event to cancel previously scheduled events. |
| 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 = "_reflex_cancel_key" | ||
| _StreamItemT = TypeVar("_StreamItemT") | ||
|
|
||
|
|
||
|
|
@@ -98,6 +99,7 @@ class EventProcessor: | |
| _root_context: The root event context to use for events enqueued without an explicit context. | ||
| _attached_root_context_token: The context variable token for the attached root context, used to reset the context variable on shutdown. | ||
| _tasks: A mapping of active transaction ids to their corresponding event handler tasks, used for tracking and cancellation on shutdown. | ||
| _cancel_keys: A mapping of ``cancel_previous_task`` keys (token, event name) to the txid of the most recently dispatched task for that key. | ||
|
tim-haselhoff marked this conversation as resolved.
|
||
| """ | ||
|
|
||
| middleware: MiddlewareMixin | None = None | ||
|
|
@@ -124,6 +126,9 @@ class EventProcessor: | |
| str, | ||
| collections.deque[tuple[EventQueueEntry, RegisteredEventHandler]], | ||
| ] = dataclasses.field(default_factory=dict, init=False) | ||
| _cancel_keys: dict[tuple[str, str], str] = dataclasses.field( | ||
| default_factory=dict, init=False | ||
| ) | ||
|
|
||
| def configure( | ||
| self, | ||
|
|
@@ -311,6 +316,7 @@ async def stop(self, graceful_shutdown_timeout: float | None = None) -> None: | |
| self._queue_task = None | ||
| # Discard any pending per-token queue entries. | ||
| self._token_queues.clear() | ||
| self._cancel_keys.clear() | ||
| # Cancel any remaining unresolved futures. | ||
| for future in self._futures.values(): | ||
| if not future.done(): | ||
|
|
@@ -579,6 +585,14 @@ 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) | ||
| if (existing_txid := self._cancel_keys.get(cancel_key)) is not None: | ||
| existing = self._tasks.get(existing_txid) | ||
| if existing is not None and not existing.done(): | ||
| existing.cancel() | ||
|
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 +601,9 @@ 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._cancel_keys[cancel_key] = entry.ctx.txid | ||
| self._tasks[entry.ctx.txid] = task | ||
| task.add_done_callback(self._finish_task) | ||
| return task | ||
|
|
@@ -714,6 +731,12 @@ def _finish_task(self, task: asyncio.Task): | |
| else: | ||
| task_ctx = task.get_context().run(EventContext.get) | ||
| self._tasks.pop(task_ctx.txid, None) | ||
| cancel_key = getattr(task, CANCEL_KEY, None) | ||
| if ( | ||
| cancel_key is not None | ||
| and self._cancel_keys.get(cancel_key) == task_ctx.txid | ||
| ): | ||
| del self._cancel_keys[cancel_key] | ||
| # Chain the next sequential event for this token if applicable. | ||
| token_queue = self._token_queues.get(task_ctx.token) | ||
| if token_queue and token_queue[0][0].ctx.txid == task_ctx.txid: | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.