Skip to content

textual.log from worker thread is not working #5791

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
dhallas opened this issue May 9, 2025 · 5 comments · Fixed by #5863
Closed

textual.log from worker thread is not working #5791

dhallas opened this issue May 9, 2025 · 5 comments · Fixed by #5863

Comments

@dhallas
Copy link
Contributor

dhallas commented May 9, 2025

Since textual 3.2.0 calling textual.log from a worker thread not longer works. If I revert to textual 3.1.1 logging from worker threads work correctly.

Here is an example:

from datetime import datetime

from textual import log, work
from textual.app import App, ComposeResult
from textual.widgets import Digits


class ClockApp(App):
    CSS = """
    Screen { align: center middle; }
    Digits { width: auto; }
    """

    def compose(self) -> ComposeResult:
        yield Digits("")

    def on_ready(self) -> None:
        self.update_clock()
        self.set_interval(1, self.update_clock)

    def update_clock(self) -> None:
        clock = datetime.now().time()
        self.query_one(Digits).update(f"{clock:%T}")
        self.worker()

    @work(thread=True)
    async def worker(self) -> None:
        log("Log message")


if __name__ == "__main__":
    app = ClockApp()
    app.run()

The Log message is correctly logged in textual 3.1.1 but is not logged in textual 3.2.0

Copy link

github-actions bot commented May 9, 2025

We found the following entries in the FAQ which you may find helpful:

Feel free to close this issue if you found an answer in the FAQ. Otherwise, please give us a little time to review.

This is an automated reply, generated by FAQtory

@TomJGooding
Copy link
Contributor

You'll need to run with TEXTUAL_DEBUG=1. From the release notes for Textual v3.2.0:

Log messages could be written to stdout when there was no app, which could happen when using run_async or threads. Now they will be suppressed, unless the env var TEXTUAL_DEBUG is set

@dhallas
Copy link
Contributor Author

dhallas commented May 10, 2025

Thanks for the clarification. I read the release notes, but did not think that this would be affected.

When thinking about it, we could make the developer user experience slightly better. What I am doing (and have been doing since starting using textual) is to run my application like this:

textual run --dev ./my_app.py

Why doesn't this automatically enable the TEXTUAL_DEBUG variable? If you think it would be a good idea, I can take a look at doing a PR.

@willmcgugan
Copy link
Collaborator

The change @TomJGooding highlighted was fixing an issue where log messages were written to stdout. The change fixed that, but breaking logging from threads was unintentional.

Fix is incoming, but note that you will need to use app.log rather than the global log.

Copy link

Don't forget to star the repository!

Follow @textualizeio for Textual updates.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants