Skip to content
This repository was archived by the owner on Nov 23, 2017. It is now read-only.

Commit ee01f37

Browse files
committed
Python issue #20505: BaseEventLoop uses again the resolution of the clock to
decide if scheduled tasks should be executed or not.
1 parent 179735c commit ee01f37

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

asyncio/base_events.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ def __init__(self):
9696
self._default_executor = None
9797
self._internal_fds = 0
9898
self._running = False
99+
self._clock_resolution = time.get_clock_info('monotonic').resolution
99100

100101
def _make_socket_transport(self, sock, protocol, waiter=None, *,
101102
extra=None, server=None):
@@ -633,14 +634,20 @@ def _run_once(self):
633634
else:
634635
logger.log(level, 'poll took %.3f seconds', t1-t0)
635636
else:
637+
t0 = self.time()
636638
event_list = self._selector.select(timeout)
639+
dt = self.time() - t0
640+
if not event_list and timeout and dt < timeout:
641+
print("asyncio: selector.select(%.3f ms) took %.3f ms"
642+
% (timeout*1e3, dt*1e3),
643+
file=sys.__stderr__, flush=True)
637644
self._process_events(event_list)
638645

639646
# Handle 'later' callbacks that are ready.
640-
now = self.time()
647+
end_time = self.time() + self._clock_resolution
641648
while self._scheduled:
642649
handle = self._scheduled[0]
643-
if handle._when > now:
650+
if handle._when >= end_time:
644651
break
645652
handle = heapq.heappop(self._scheduled)
646653
self._ready.append(handle)

tests/test_events.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1176,13 +1176,18 @@ def wait():
11761176
loop = self.loop
11771177
yield from asyncio.sleep(1e-2, loop=loop)
11781178
yield from asyncio.sleep(1e-4, loop=loop)
1179+
yield from asyncio.sleep(1e-6, loop=loop)
1180+
yield from asyncio.sleep(1e-8, loop=loop)
1181+
yield from asyncio.sleep(1e-10, loop=loop)
11791182

11801183
self.loop.run_until_complete(wait())
1181-
# The ideal number of call is 6, but on some platforms, the selector
1184+
# The ideal number of call is 12, but on some platforms, the selector
11821185
# may sleep at little bit less than timeout depending on the resolution
1183-
# of the clock used by the kernel. Tolerate 2 useless calls on these
1184-
# platforms.
1185-
self.assertLessEqual(self.loop._run_once_counter, 8)
1186+
# of the clock used by the kernel. Tolerate a few useless calls on
1187+
# these platforms.
1188+
self.assertLessEqual(self.loop._run_once_counter, 20,
1189+
{'clock_resolution': self.loop._clock_resolution,
1190+
'selector': self.loop._selector.__class__.__name__})
11861191

11871192

11881193
class SubprocessTestsMixin:

0 commit comments

Comments
 (0)