Skip to content

Commit 85582a4

Browse files
committed
timer fix for Windoze
1 parent 38858e4 commit 85582a4

File tree

2 files changed

+17
-16
lines changed

2 files changed

+17
-16
lines changed

src/textual/_time.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,18 @@
1414

1515

1616
if WINDOWS:
17-
async def sleep(sleep_for:float) -> None:
17+
18+
async def sleep(sleep_for: float) -> None:
19+
"""An asyncio sleep.
20+
21+
On Windows this achieves a better granularity that asyncio.sleep(0)
22+
23+
Args:
24+
sleep_for (float): Seconds to sleep for.
25+
"""
1826
start = time()
19-
while time() - start < sleep_for - 1/1000:
27+
while time() - start < sleep_for - 1 / 1000:
2028
await asleep(0)
21-
29+
2230
else:
2331
sleep = asleep

src/textual/timer.py

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,16 @@
99

1010
import asyncio
1111
import weakref
12-
from asyncio import (
13-
CancelledError,
14-
Event,
15-
Task,
16-
)
12+
from asyncio import CancelledError, Event, Task
1713
from typing import Awaitable, Callable, Union
1814

19-
2015
from rich.repr import Result, rich_repr
2116

22-
from . import events
17+
from . import _clock, events
2318
from ._callback import invoke
2419
from ._context import active_app
25-
from . import _clock
26-
from ._types import MessageTarget
2720
from ._time import sleep
21+
from ._types import MessageTarget
2822

2923
TimerCallback = Union[Callable[[], Awaitable[None]], Callable[[], None]]
3024

@@ -142,7 +136,7 @@ async def _run(self) -> None:
142136
_interval = self._interval
143137
await self._active.wait()
144138
start = _clock.get_time_no_wait()
145-
sleep_event = Event()
139+
146140
while _repeat is None or count <= _repeat:
147141
next_timer = start + ((count + 1) * _interval)
148142
now = await _clock.get_time()
@@ -151,9 +145,8 @@ async def _run(self) -> None:
151145
continue
152146
now = await _clock.get_time()
153147
wait_time = max(0, next_timer - now)
154-
if wait_time > 1/1000:
155-
await sleep(wait_time)
156-
148+
if wait_time > 1 / 1000:
149+
await sleep(wait_time)
157150

158151
count += 1
159152
await self._active.wait()

0 commit comments

Comments
 (0)