Skip to content
This repository was archived by the owner on Apr 28, 2026. It is now read-only.

Commit 221cf8f

Browse files
committed
Revert "fixes issue #1200 (#1256)"
This reverts commit 6182440.
1 parent d3f9230 commit 221cf8f

1 file changed

Lines changed: 0 additions & 31 deletions

File tree

docs/events/background_events.md

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -104,34 +104,6 @@ def background_task_example():
104104
)
105105
```
106106

107-
## Terminating Background Tasks on Page Close or Navigation
108-
109-
Sometimes, background tasks may continue running even after the user navigates away from the page or closes the browser tab. To handle such cases, you can check if the websocket associated with the state is disconnected and terminate the background task when necessary.
110-
111-
The solution involves checking if the client_token is still valid in the app.event_namespace.token_to_sid mapping. If the session is lost (e.g., the user navigates away or closes the page), the background task will stop.
112-
113-
```python
114-
import asyncio
115-
import reflex as rx
116-
117-
class State(rx.State):
118-
@rx.event(background=True)
119-
async def loop_function(self):
120-
while True:
121-
if self.router.session.client_token not in app.event_namespace.token_to_sid:
122-
print("WebSocket connection closed or user navigated away. Stopping background task.")
123-
break
124-
125-
print("Running background task...")
126-
await asyncio.sleep(2)
127-
128-
129-
@rx.page(on_load=State.loop_function)
130-
def index():
131-
return rx.text("Hello, this page will manage background tasks and stop them when the page is closed or navigated away.")
132-
133-
```
134-
135107
## Task Lifecycle
136108

137109
When a background task is triggered, it starts immediately, saving a reference to
@@ -146,9 +118,6 @@ the circumstances that are undesirable. In the example above, the `_n_tasks`
146118
backend var is used to control whether `my_task` will enter the increment loop,
147119
or exit early.
148120

149-
150-
151-
152121
## Background Task Limitations
153122

154123
Background tasks mostly work like normal `EventHandler` methods, with certain exceptions:

0 commit comments

Comments
 (0)