This repository has been archived by the owner on Jun 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Add an example for Textualize/textual#3472
- Loading branch information
Showing
1 changed file
with
47 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
"""Example for https://github.com/Textualize/textual/issues/3472""" | ||
|
||
from time import sleep | ||
|
||
from textual import on, work | ||
from textual.app import App, ComposeResult | ||
from textual.widgets import Button, Log | ||
from textual.worker import get_current_worker | ||
|
||
class WorkerInWorkerApp(App[None]): | ||
|
||
def compose(self) -> ComposeResult: | ||
yield Button("Start") | ||
yield Log() | ||
|
||
@work(thread=True) | ||
def outer(self) -> None: | ||
worker = get_current_worker() | ||
for n in range(50): | ||
if worker.is_cancelled: | ||
break | ||
if n == 10: | ||
self.call_from_thread(self.inner) | ||
self.call_from_thread( | ||
self.query_one(Log).write_line, | ||
f"Outer {n}" | ||
) | ||
sleep(0.25) | ||
|
||
@work(thread=True) | ||
def inner(self) -> None: | ||
worker = get_current_worker() | ||
for n in range(50): | ||
if worker.is_cancelled: | ||
break | ||
self.call_from_thread( | ||
self.query_one(Log).write_line, | ||
f"Inner {n}" | ||
) | ||
sleep(0.25) | ||
|
||
@on(Button.Pressed) | ||
def start(self) -> None: | ||
self.outer() | ||
|
||
if __name__ == "__main__": | ||
WorkerInWorkerApp().run() |