Skip to content
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

Add Warning for Long-Running Message Handlers in Debug Mode #1

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/textual/message_pump.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@
from __future__ import annotations

import asyncio
import os
import threading
from asyncio import CancelledError, Queue, QueueEmpty, Task, create_task
from contextlib import contextmanager
from functools import partial
from time import perf_counter
from typing import (
TYPE_CHECKING,
Any,
Expand Down Expand Up @@ -48,6 +50,7 @@
from .app import App
from .css.model import SelectorSet

SLOW_THRESHOLD_MS = int(os.getenv('TEXTUAL_SLOW_THRESHOLD_MS', 500))

Callback: TypeAlias = "Callable[..., Any] | Callable[..., Awaitable[Any]]"

Expand Down Expand Up @@ -655,6 +658,16 @@ async def _dispatch_message(self, message: Message) -> None:
# Allow apps to treat events and messages separately
if isinstance(message, Event):
await self.on_event(message)
elif not isinstance(message, Event) and "debug" in self.app.features:
start = perf_counter()
await self._on_message(message)
if perf_counter() - start > SLOW_THRESHOLD_MS / 1000:
log.warning.verbosity(message.verbose)(
f"method=<{self.__class__.__name__}.{message.handler_name}>",
">>>",
f"Took over {SLOW_THRESHOLD_MS}ms to process.",
"\nTo avoid screen freezes use the [work decorator](/guide/workers/#workers)"
)
else:
await self._on_message(message)
if self._next_callbacks:
Expand Down