def call_later( cb: Callable[[], Any], timeout: float, loop: asyncio.AbstractEventLoop) -> Optional[asyncio.TimerHandle]: if timeout is not None and timeout > 0: when = loop.time() + timeout if timeout > 5: when = ceil(when) return loop.call_at(when, cb) return None
def weakref_handle( ob: object, name: str, timeout: float, loop: asyncio.AbstractEventLoop) -> Optional[asyncio.TimerHandle]: if timeout is not None and timeout > 0: when = loop.time() + timeout if timeout >= 5: when = ceil(when) return loop.call_at(when, _weakref_handle, (weakref.ref(ob), name)) return None
def set_timeout(task: asyncio.Task, timeout: [float, int], loop: asyncio.AbstractEventLoop = None, timeout_cancel=True): assert isinstance(timeout, (float, int)) if loop is None: loop = get_running_loop() now_time = loop.time() out_time = now_time + timeout if timeout_cancel: if timeout <= 0: task.cancel() return unset_timeout(task) handle = loop.call_at(out_time, task.cancel) setattr(task, _MODULE_TIMEOUT_HANDLE, handle) setattr(task, _MODULE_TIMEOUT, out_time)