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.04k stars 765 forks source link

Log slow message processing #4895

Closed willmcgugan closed 2 days ago

willmcgugan commented 3 weeks ago

If processing messages take longer than a fraction of a second, it can cause the UI to freeze.

This is usually the result blocking operations or cpu intensive work in a message handler. The solution is typically to do the work in a worker, but devs may not know to do that.

Given its a gotcha, I think Textual can do a little more to help.

I think what we should do is time how long a message has taken to process. If it is longer than, say 500ms, it should log a warning.

This should only be enabled in debug mode. The maximum time should be set in an environment variable.

I suspect, the best place to do this would be https://github.com/Textualize/textual/blob/main/src/textual/message_pump.py#L637

Requirements:

Bonus points

Strictly following the above, would not produce a warning until the handler returns. If it takes 30 seconds, the dev would not see anything.

It would be great if long running message handlers produce a warning even before the handler returns. This would be a little trickier to implement. And it should be done in a way that doesn't slow down non-debug runs.

Zimzozaur commented 3 weeks ago

Let me give this a shot 🙂

willmcgugan commented 3 weeks ago

Assigned to you. Let us know how it goes.

github-actions[bot] commented 2 days ago

Don't forget to star the repository!

Follow @textualizeio for Textual updates.