textual icon indicating copy to clipboard operation
textual copied to clipboard

Log slow message processing

Open willmcgugan opened this issue 1 year ago • 2 comments

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:

  • Display warning for message handers that take a long time
  • Enable in debug mode
  • Add env var to set the time that produces a warning
  • Should not add to much cpu work to message processing

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.

willmcgugan avatar Aug 18 '24 15:08 willmcgugan

Let me give this a shot 🙂

Piechutowski avatar Aug 19 '24 13:08 Piechutowski

Assigned to you. Let us know how it goes.

willmcgugan avatar Aug 19 '24 17:08 willmcgugan

Don't forget to star the repository!

Follow @textualizeio for Textual updates.

github-actions[bot] avatar Sep 11 '24 14:09 github-actions[bot]