-
In the following ExampleApp, I would expect that when the app is quit, the thread worker would terminate too. It does not, and instead the process runs forever. Here's the example code. It continues running even after quitting the app with ctrl-C. import time
from textual import work
from textual.app import App
from textual.app import ComposeResult
from textual.messages import Message
from textual.widget import Widget
from textual.widgets import Log
class ExampleWidget(Widget):
class Changed(Message):
def __init__(self, name: str):
super().__init__()
self.name = name
def on_mount(self):
self.start_worker()
@work(thread=True)
def start_worker(self) -> None:
while True:
time.sleep(1)
self.post_message(self.Changed("And again..."))
class ExampleApp(App):
def compose(self) -> ComposeResult:
yield Log()
yield ExampleWidget()
if __name__ == "__main__":
app = ExampleApp()
app.run() Surprisingly (to me) if I try to minimize the example further, then it starts working as intended and the bug goes away. In the following code, the app successfully quits on ctrl-C. The difference is that the thread worker is in the App rather than in a widget. import time
from textual import work
from textual.app import App
from textual.app import ComposeResult
from textual.widgets import Log
class ExampleApp(App):
def compose(self) -> ComposeResult:
yield Log()
def on_mount(self):
self.start_worker()
@work(thread=True)
def start_worker(self):
log = self.query_one(Log)
while True:
log.write_line("And again...")
time.sleep(1)
if __name__ == "__main__":
app = ExampleApp()
app.run() I did notice that in #2593 @davep notes that long-running thread workers are a bad idea. I haven't looked into alternatives yet. If you have a recommendation or rationale, do let me know. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
It might be worth having a glance back over the guide to workers, and the section on threaded workers and note the use of The issue you link to is, if I recall correctly, more about not allowing long-running thread workers to stack up, because eventually you'll have too many threads running. |
Beta Was this translation helpful? Give feedback.
It might be worth having a glance back over the guide to workers, and the section on threaded workers and note the use of
get_current_worker
to get the worker and then checking if the workeris_cancelled
in the busy loop.The issue you link to is, if I recall correctly, more about not allowing long-running thread workers to stack up, because eventually you'll have too many threads running.