1 """Timer for asyncio."""
16 An asynchronous Timer object.
20 timeout: :class:`float`:
21 The duration for which the timer should last.
23 repeat: :class:`bool`:
24 Whether the timer should repeat.
26 callback: :class:`Coroutine` or `Method` or `Function`:
27 An `asyncio` coroutine or a regular method that will be called as soon as
30 callback_args: Optional[:class:`tuple`]:
31 The args to be passed to the callback.
33 callback_kwargs: Optional[:class:`dict`]:
34 The kwargs to be passed to the callback.
37 self
._timeout
= timeout
39 self
._callback
= callback
40 self
._task
= asyncio
.create_task(self
._job
())
41 self
._callback
_args
= callback_args
42 if callback_kwargs
is None:
44 self
._callback
_kwargs
= callback_kwargs
48 while self
._task
.cancelled() is False:
49 await asyncio
.sleep(self
._timeout
)
50 await self
._call
_callback
()
52 await asyncio
.sleep(self
._timeout
)
53 await self
._call
_callback
()
55 async def _call_callback(self
):
56 if asyncio
.iscoroutine(self
._callback
) or asyncio
.iscoroutinefunction(
59 await self
._callback
(*self
._callback
_args
, **self
._callback
_kwargs
)
61 self
._callback
(*self
._callback
_args
, **self
._callback
_kwargs
)
64 """Cancels the timer. The callback will not be called."""