Skip to content

Commit 3d4dbd8

Browse files
authored
Implement TimerHandle.when() (#5473)
1 parent 83ab995 commit 3d4dbd8

File tree

4 files changed

+34
-4
lines changed

4 files changed

+34
-4
lines changed

Doc/library/asyncio-eventloop.rst

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ a different clock than :func:`time.time`.
171171
Arrange for the *callback* to be called after the given *delay*
172172
seconds (either an int or float).
173173

174-
An instance of :class:`asyncio.Handle` is returned, which can be
174+
An instance of :class:`asyncio.TimerHandle` is returned, which can be
175175
used to cancel the callback.
176176

177177
*callback* will be called exactly once per call to :meth:`call_later`.
@@ -193,7 +193,7 @@ a different clock than :func:`time.time`.
193193

194194
This method's behavior is the same as :meth:`call_later`.
195195

196-
An instance of :class:`asyncio.Handle` is returned, which can be
196+
An instance of :class:`asyncio.TimerHandle` is returned, which can be
197197
used to cancel the callback.
198198

199199
:ref:`Use functools.partial to pass keywords to the callback
@@ -1076,8 +1076,7 @@ Handle
10761076
.. class:: Handle
10771077

10781078
A callback wrapper object returned by :func:`AbstractEventLoop.call_soon`,
1079-
:func:`AbstractEventLoop.call_soon_threadsafe`, :func:`AbstractEventLoop.call_later`,
1080-
and :func:`AbstractEventLoop.call_at`.
1079+
:func:`AbstractEventLoop.call_soon_threadsafe`.
10811080

10821081
.. method:: cancel()
10831082

@@ -1090,6 +1089,22 @@ Handle
10901089

10911090
.. versionadded:: 3.7
10921091

1092+
.. class:: TimerHandle
1093+
1094+
A callback wrapper object returned by :func:`AbstractEventLoop.call_later`,
1095+
and :func:`AbstractEventLoop.call_at`.
1096+
1097+
The class is inherited from :class:`Handle`.
1098+
1099+
.. method:: when()
1100+
1101+
Return a scheduled callback time as :class:`float` seconds.
1102+
1103+
The time is an absolute timestamp, using the same time
1104+
reference as :meth:`AbstractEventLoop.time`.
1105+
1106+
.. versionadded:: 3.7
1107+
10931108

10941109
SendfileNotAvailableError
10951110
-------------------------

Lib/asyncio/events.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,14 @@ def cancel(self):
156156
self._loop._timer_handle_cancelled(self)
157157
super().cancel()
158158

159+
def when(self):
160+
"""Return a scheduled callback time.
161+
162+
The time is an absolute timestamp, using the same time
163+
reference as loop.time().
164+
"""
165+
return self._when
166+
159167

160168
class AbstractServer:
161169
"""Abstract server returned by create_server()."""

Lib/test/test_asyncio/test_events.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2679,6 +2679,12 @@ def test_hash(self):
26792679
mock.Mock())
26802680
self.assertEqual(hash(h), hash(when))
26812681

2682+
def test_when(self):
2683+
when = time.monotonic()
2684+
h = asyncio.TimerHandle(when, lambda: False, (),
2685+
mock.Mock())
2686+
self.assertEqual(when, h.when())
2687+
26822688
def test_timer(self):
26832689
def callback(*args):
26842690
return args
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Implement ``asyncio.TimerHandle.when()`` method.

0 commit comments

Comments
 (0)