Skip to content

Commit a61b33b

Browse files
asvetlovclaude
andcommitted
gh-161032: Convert BaseEventLoop._clock_resolution to asyncio.constants.CLOCK_RESOLUTION
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent af79f49 commit a61b33b

5 files changed

Lines changed: 9 additions & 4 deletions

File tree

‎Lib/asyncio/base_events.py‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,6 @@ def __init__(self):
432432
# Identifier of the thread running the event loop, or None if the
433433
# event loop is not running
434434
self._thread_id = None
435-
self._clock_resolution = time.get_clock_info('monotonic').resolution
436435
self._exception_handler = None
437436
self.set_debug(coroutines._is_debug_mode())
438437
# The preserved state of async generator hooks.
@@ -2057,7 +2056,7 @@ def _run_once(self):
20572056
now = self.time()
20582057
# Ensure that `end_time` is strictly increasing
20592058
# when the clock resolution is too small.
2060-
end_time = now + max(self._clock_resolution, math.ulp(now))
2059+
end_time = now + max(constants.CLOCK_RESOLUTION, math.ulp(now))
20612060
while self._scheduled:
20622061
handle = self._scheduled[0]
20632062
if handle._when >= end_time:

‎Lib/asyncio/constants.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# SPDX-FileCopyrightText: Copyright (c) 2015-2021 MagicStack Inc. http://magic.io
44

55
import enum
6+
import time
67

78
# After the connection is lost, log warnings after this many write()s.
89
LOG_THRESHOLD_FOR_CONNLOST_WRITES = 5
@@ -33,6 +34,9 @@
3334
# Default timeout for joining the threads in the threadpool
3435
THREAD_JOIN_TIMEOUT = 300
3536

37+
# Resolution of the clock used by the event loop, in seconds.
38+
CLOCK_RESOLUTION = time.get_clock_info('monotonic').resolution
39+
3640
# The enum should be here to break circular dependencies between
3741
# base_events and sslproto
3842
class _SendfileMode(enum.Enum):

‎Lib/test/test_asyncio/test_events.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import tty
2828

2929
import asyncio
30+
from asyncio import constants
3031
from asyncio import coroutines
3132
from asyncio import events
3233
from asyncio import selector_events
@@ -2314,7 +2315,7 @@ async def wait():
23142315
# of the clock used by the kernel. Tolerate a few useless calls on
23152316
# these platforms.
23162317
self.assertLessEqual(self.loop._run_once_counter, 20,
2317-
{'clock_resolution': self.loop._clock_resolution,
2318+
{'clock_resolution': constants.CLOCK_RESOLUTION,
23182319
'selector': self.loop._selector.__class__.__name__})
23192320

23202321
def test_remove_fds_after_closing(self):

‎Lib/test/test_asyncio/utils.py‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,6 @@ def gen():
360360
self._gen = gen()
361361
next(self._gen)
362362
self._time = 0
363-
self._clock_resolution = 1e-9
364363
self._timers = []
365364
self._selector = TestSelector()
366365

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
:mod:`asyncio`: The resolution of the monotonic clock is now computed once at
2+
import time instead of in every event loop.

0 commit comments

Comments
 (0)