1 """Timer for asyncio."""
15 """An asynchronous Timer object.
19 timeout: :class:`float`:
20 The duration for which the timer should last.
22 repeat: :class:`bool`:
23 Whether the timer should repeat.
25 callback: :class:`Coroutine` or `Method` or `Function`:
26 An `asyncio` coroutine or a regular method that will be called as soon as
29 callback_args: Optional[:class:`tuple`]:
30 The args to be passed to the callback.
32 callback_kwargs: Optional[:class:`dict`]:
33 The kwargs to be passed to the callback.
36 self
._timeout
= timeout
38 self
._callback
= callback
39 self
._task
= asyncio
.create_task(self
._job
())
40 self
._callback
_args
= callback_args
41 if callback_kwargs
is None:
43 self
._callback
_kwargs
= callback_kwargs
47 while self
._task
.cancelled() is False:
48 await asyncio
.sleep(self
._timeout
)
49 await self
._call
_callback
()
51 await asyncio
.sleep(self
._timeout
)
52 await self
._call
_callback
()
54 async def _call_callback(self
):
55 if asyncio
.iscoroutine(self
._callback
) or asyncio
.iscoroutinefunction(
58 await self
._callback
(*self
._callback
_args
, **self
._callback
_kwargs
)
60 self
._callback
(*self
._callback
_args
, **self
._callback
_kwargs
)
63 """Cancels the timer. The callback will not be called."""