Textualize / textual

The lean application framework for Python. Build sophisticated user interfaces with a simple Python API. Run your apps in the terminal and a web browser.
https://textual.textualize.io/
MIT License
25.19k stars 772 forks source link

Workers report RUNNING state twice #4670

Closed Joe-TheBro closed 3 months ago

Joe-TheBro commented 3 months ago

Have you checked closed issues? https://github.com/Textualize/textual/issues?q=is%3Aissue+is%3Aclosed Yes

The worker reports a RUNNING state when PENDING, causing a double report when the worker is actually in a RUNNING state. image

There is no error shown on the command line, and TUI launches fine.

I'm wondering if this is the intended behavior, as I wrote some code similar to the code below surrounding the Worker.StateChanged event.

def on_worker_state_changed(self, event: Worker.StateChanged) -> None:
        worker_name = event.worker.name
        worker_state = str(event.worker.state).split(".")[1]
        self.log(event.worker.state)
        match worker_name:
            case "get_arduino":
                match worker_state:
                    case "PENDING":
                        pass
                    case "RUNNING":
                        pass
                    case "CANCELLED":
                        pass
                    case "ERROR":
                        pass
                    case "SUCCESS":
                        pass

Textual Diagnostics

Versions

Name Value
Textual 0.67.1
Rich 13.7.1

Python

Name Value
Version 3.12.3
Implementation CPython
Compiler MSC v.1938 64 bit (AMD64)
Executable \env\python.exe

Operating System

Name Value
System Windows
Release 11
Version 10.0.22631

Terminal

Name Value
Terminal Application vscode (1.90.1)
TERM Not set
COLORTERM truecolor
FORCE_COLOR Not set
NO_COLOR Not set

Rich Console options

Name Value
size width=159, height=27
legacy_windows False
min_width 1
max_width 159
is_terminal True
encoding utf-8
max_height 27
justify None
overflow None
no_wrap False
highlight None
markup None
height None
github-actions[bot] commented 3 months ago

Thank you for your issue. Give us a little time to review it.

PS. You might want to check the FAQ if you haven't done so already.

This is an automated reply, generated by FAQtory

willmcgugan commented 3 months ago

I suspect what you are seeing is that by the time the event is received, the state has change.

The StateChange event has a state property that contains the state when the event was posted. If you log that, you may find it works as expected.

Joe-TheBro commented 3 months ago

I appreciate the quick response. This was exactly what I was looking for. I realize now that when I set worker_state = event.worker.state, it was referencing the worker itself. Because that takes time, there is no guarantee that it will be the state advertised in the event. Thanks!

github-actions[bot] commented 3 months ago

Don't forget to star the repository!

Follow @textualizeio for Textual updates.