Skip to content
This repository has been archived by the owner on Jun 21, 2024. It is now read-only.

Commit

Permalink
✨ Add an example for Textualize/textual#3472
Browse files Browse the repository at this point in the history
  • Loading branch information
davep committed Oct 6, 2023
1 parent d391b01 commit 9f487a0
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions work_from_work.py
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()

0 comments on commit 9f487a0

Please sign in to comment.