mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
Implement TimerHandle.when() (#5473)
This commit is contained in:
parent
83ab995871
commit
3d4dbd8f01
4 changed files with 34 additions and 4 deletions
|
@ -171,7 +171,7 @@ a different clock than :func:`time.time`.
|
||||||
Arrange for the *callback* to be called after the given *delay*
|
Arrange for the *callback* to be called after the given *delay*
|
||||||
seconds (either an int or float).
|
seconds (either an int or float).
|
||||||
|
|
||||||
An instance of :class:`asyncio.Handle` is returned, which can be
|
An instance of :class:`asyncio.TimerHandle` is returned, which can be
|
||||||
used to cancel the callback.
|
used to cancel the callback.
|
||||||
|
|
||||||
*callback* will be called exactly once per call to :meth:`call_later`.
|
*callback* will be called exactly once per call to :meth:`call_later`.
|
||||||
|
@ -193,7 +193,7 @@ a different clock than :func:`time.time`.
|
||||||
|
|
||||||
This method's behavior is the same as :meth:`call_later`.
|
This method's behavior is the same as :meth:`call_later`.
|
||||||
|
|
||||||
An instance of :class:`asyncio.Handle` is returned, which can be
|
An instance of :class:`asyncio.TimerHandle` is returned, which can be
|
||||||
used to cancel the callback.
|
used to cancel the callback.
|
||||||
|
|
||||||
:ref:`Use functools.partial to pass keywords to the callback
|
:ref:`Use functools.partial to pass keywords to the callback
|
||||||
|
@ -1076,8 +1076,7 @@ Handle
|
||||||
.. class:: Handle
|
.. class:: Handle
|
||||||
|
|
||||||
A callback wrapper object returned by :func:`AbstractEventLoop.call_soon`,
|
A callback wrapper object returned by :func:`AbstractEventLoop.call_soon`,
|
||||||
:func:`AbstractEventLoop.call_soon_threadsafe`, :func:`AbstractEventLoop.call_later`,
|
:func:`AbstractEventLoop.call_soon_threadsafe`.
|
||||||
and :func:`AbstractEventLoop.call_at`.
|
|
||||||
|
|
||||||
.. method:: cancel()
|
.. method:: cancel()
|
||||||
|
|
||||||
|
@ -1090,6 +1089,22 @@ Handle
|
||||||
|
|
||||||
.. versionadded:: 3.7
|
.. versionadded:: 3.7
|
||||||
|
|
||||||
|
.. class:: TimerHandle
|
||||||
|
|
||||||
|
A callback wrapper object returned by :func:`AbstractEventLoop.call_later`,
|
||||||
|
and :func:`AbstractEventLoop.call_at`.
|
||||||
|
|
||||||
|
The class is inherited from :class:`Handle`.
|
||||||
|
|
||||||
|
.. method:: when()
|
||||||
|
|
||||||
|
Return a scheduled callback time as :class:`float` seconds.
|
||||||
|
|
||||||
|
The time is an absolute timestamp, using the same time
|
||||||
|
reference as :meth:`AbstractEventLoop.time`.
|
||||||
|
|
||||||
|
.. versionadded:: 3.7
|
||||||
|
|
||||||
|
|
||||||
SendfileNotAvailableError
|
SendfileNotAvailableError
|
||||||
-------------------------
|
-------------------------
|
||||||
|
|
|
@ -156,6 +156,14 @@ class TimerHandle(Handle):
|
||||||
self._loop._timer_handle_cancelled(self)
|
self._loop._timer_handle_cancelled(self)
|
||||||
super().cancel()
|
super().cancel()
|
||||||
|
|
||||||
|
def when(self):
|
||||||
|
"""Return a scheduled callback time.
|
||||||
|
|
||||||
|
The time is an absolute timestamp, using the same time
|
||||||
|
reference as loop.time().
|
||||||
|
"""
|
||||||
|
return self._when
|
||||||
|
|
||||||
|
|
||||||
class AbstractServer:
|
class AbstractServer:
|
||||||
"""Abstract server returned by create_server()."""
|
"""Abstract server returned by create_server()."""
|
||||||
|
|
|
@ -2679,6 +2679,12 @@ class TimerTests(unittest.TestCase):
|
||||||
mock.Mock())
|
mock.Mock())
|
||||||
self.assertEqual(hash(h), hash(when))
|
self.assertEqual(hash(h), hash(when))
|
||||||
|
|
||||||
|
def test_when(self):
|
||||||
|
when = time.monotonic()
|
||||||
|
h = asyncio.TimerHandle(when, lambda: False, (),
|
||||||
|
mock.Mock())
|
||||||
|
self.assertEqual(when, h.when())
|
||||||
|
|
||||||
def test_timer(self):
|
def test_timer(self):
|
||||||
def callback(*args):
|
def callback(*args):
|
||||||
return args
|
return args
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
Implement ``asyncio.TimerHandle.when()`` method.
|
Loading…
Add table
Add a link
Reference in a new issue