Skip to content

Commit 5914f57

Browse files
committed
message pump optimization
1 parent 2898b2a commit 5914f57

File tree

3 files changed

+54
-4
lines changed

3 files changed

+54
-4
lines changed

src/textual/_queue.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
from __future__ import annotations
2+
3+
import asyncio
4+
from asyncio import Event
5+
from collections import deque
6+
from typing import Generic, TypeVar
7+
8+
QueueType = TypeVar("QueueType")
9+
10+
11+
class Queue(Generic[QueueType]):
12+
"""A cut-down version of asyncio.Queue
13+
14+
This has just enough functionality to run the message pumps.
15+
16+
"""
17+
18+
def __init__(self) -> None:
19+
self.values: deque[QueueType] = deque()
20+
self.ready_event = Event()
21+
22+
def put_nowait(self, value: QueueType) -> None:
23+
self.values.append(value)
24+
self.ready_event.set()
25+
26+
def qsize(self) -> int:
27+
return len(self.values)
28+
29+
def empty(self) -> bool:
30+
return not self.values
31+
32+
def task_done(self) -> None:
33+
pass
34+
35+
async def get(self) -> QueueType:
36+
if not self.ready_event.is_set():
37+
await self.ready_event.wait()
38+
value = self.values.popleft()
39+
if not self.values:
40+
self.ready_event.clear()
41+
return value
42+
43+
def get_nowait(self) -> QueueType:
44+
if not self.values:
45+
raise asyncio.QueueEmpty()
46+
value = self.values.popleft()
47+
if not self.values:
48+
self.ready_event.clear()
49+
return value

src/textual/message_pump.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
from textual._context import message_hook as message_hook_context_var
3737
from textual._context import prevent_message_types_stack
3838
from textual._on import OnNoWidget
39+
from textual._queue import Queue
3940
from textual._time import time
4041
from textual.constants import SLOW_THRESHOLD
4142
from textual.css.match import match
@@ -143,8 +144,8 @@ def __init__(self, parent: MessagePump | None = None) -> None:
143144
"""
144145

145146
@cached_property
146-
def _message_queue(self) -> asyncio.Queue[Message | None]:
147-
return asyncio.Queue()
147+
def _message_queue(self) -> Queue[Message | None]:
148+
return Queue()
148149

149150
@cached_property
150151
def _mounted_event(self) -> asyncio.Event:

src/textual/widget.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3904,12 +3904,12 @@ def post_render(
39043904

39053905
return renderable
39063906

3907-
def watch_mouse_hover(self, value: bool) -> None:
3907+
def watch_mouse_hover(self, _mouse_over: bool) -> None:
39083908
"""Update from CSS if mouse over state changes."""
39093909
if self._has_hover_style:
39103910
self._update_styles()
39113911

3912-
def watch_has_focus(self, value: bool) -> None:
3912+
def watch_has_focus(self, _has_focus: bool) -> None:
39133913
"""Update from CSS if has focus state changes."""
39143914
self._update_styles()
39153915

0 commit comments

Comments
 (0)