34 items
NAME ↑ SIZE MODIFIED PERMS ACTIONS
.. / Parent Directory
__pycache__ — dir
2026-07-01 11:10 · rwxr-xr-x
2026-07-01 11:10 rwxr-xr-x
__init__.py — 1.16 KB
2025-06-03 18:38 · rw-r--r--
1.16 KB 2025-06-03 18:38 rw-r--r--
__main__.py — 3.3 KB
2025-06-03 18:38 · rw-r--r--
3.3 KB 2025-06-03 18:38 rw-r--r--
base_events.py — 73.62 KB
2025-06-03 18:38 · rw-r--r--
73.62 KB 2025-06-03 18:38 rw-r--r--
base_futures.py — 1.96 KB
2025-06-03 18:38 · rw-r--r--
1.96 KB 2025-06-03 18:38 rw-r--r--
base_subprocess.py — 8.66 KB
2025-06-03 18:38 · rw-r--r--
8.66 KB 2025-06-03 18:38 rw-r--r--
base_tasks.py — 2.58 KB
2025-06-03 18:38 · rw-r--r--
2.58 KB 2025-06-03 18:38 rw-r--r--
constants.py — 1.29 KB
2025-06-03 18:38 · rw-r--r--
1.29 KB 2025-06-03 18:38 rw-r--r--
coroutines.py — 3.32 KB
2025-06-03 18:38 · rw-r--r--
3.32 KB 2025-06-03 18:38 rw-r--r--
events.py — 27.97 KB
2025-06-03 18:38 · rw-r--r--
27.97 KB 2025-06-03 18:38 rw-r--r--
exceptions.py — 1.71 KB
2025-06-03 18:38 · rw-r--r--
1.71 KB 2025-06-03 18:38 rw-r--r--
format_helpers.py — 2.35 KB
2025-06-03 18:38 · rw-r--r--
2.35 KB 2025-06-03 18:38 rw-r--r--
futures.py — 13.88 KB
2025-06-03 18:38 · rw-r--r--
13.88 KB 2025-06-03 18:38 rw-r--r--
locks.py — 18.57 KB
2025-06-03 18:38 · rw-r--r--
18.57 KB 2025-06-03 18:38 rw-r--r--
log.py — 124 B
2025-06-03 18:38 · rw-r--r--
124 B 2025-06-03 18:38 rw-r--r--
mixins.py — 481 B
2025-06-03 18:38 · rw-r--r--
481 B 2025-06-03 18:38 rw-r--r--
proactor_events.py — 32.48 KB
2025-06-03 18:38 · rw-r--r--
32.48 KB 2025-06-03 18:38 rw-r--r--
protocols.py — 6.79 KB
2025-06-03 18:38 · rw-r--r--
6.79 KB 2025-06-03 18:38 rw-r--r--
queues.py — 7.79 KB
2025-06-03 18:38 · rw-r--r--
7.79 KB 2025-06-03 18:38 rw-r--r--
runners.py — 6.68 KB
2025-06-03 18:38 · rw-r--r--
6.68 KB 2025-06-03 18:38 rw-r--r--
selector_events.py — 44.34 KB
2025-06-03 18:38 · rw-r--r--
44.34 KB 2025-06-03 18:38 rw-r--r--
sslproto.py — 31 KB
2025-06-03 18:38 · rw-r--r--
31 KB 2025-06-03 18:38 rw-r--r--
staggered.py — 5.85 KB
2025-06-03 18:38 · rw-r--r--
5.85 KB 2025-06-03 18:38 rw-r--r--
streams.py — 26.86 KB
2025-06-03 18:38 · rw-r--r--
26.86 KB 2025-06-03 18:38 rw-r--r--
subprocess.py — 7.5 KB
2025-06-03 18:38 · rw-r--r--
7.5 KB 2025-06-03 18:38 rw-r--r--
taskgroups.py — 8.27 KB
2025-06-03 18:38 · rw-r--r--
8.27 KB 2025-06-03 18:38 rw-r--r--
tasks.py — 33.63 KB
2025-06-03 18:38 · rw-r--r--
33.63 KB 2025-06-03 18:38 rw-r--r--
threads.py — 790 B
2025-06-03 18:38 · rw-r--r--
790 B 2025-06-03 18:38 rw-r--r--
timeouts.py — 5.2 KB
2025-06-03 18:38 · rw-r--r--
5.2 KB 2025-06-03 18:38 rw-r--r--
transports.py — 10.47 KB
2025-06-03 18:38 · rw-r--r--
10.47 KB 2025-06-03 18:38 rw-r--r--
trsock.py — 2.42 KB
2025-06-03 18:38 · rw-r--r--
2.42 KB 2025-06-03 18:38 rw-r--r--
unix_events.py — 50.7 KB
2025-06-03 18:38 · rw-r--r--
50.7 KB 2025-06-03 18:38 rw-r--r--
windows_events.py — 33.88 KB
2025-06-03 18:38 · rw-r--r--
33.88 KB 2025-06-03 18:38 rw-r--r--
windows_utils.py — 4.94 KB
2025-06-03 18:38 · rw-r--r--
4.94 KB 2025-06-03 18:38 rw-r--r--
ONLINE
asyncio
34 items
22:25:54
TERMINAL FM
Edit
Preview
Download
Rename
Copy
Chmod
Delete
import enum from types import TracebackType from typing import final, Optional, Type from . import events from . import exceptions from . import tasks __all__ = ( "Timeout", "timeout", "timeout_at", ) class _State(enum.Enum): CREATED = "created" ENTERED = "active" EXPIRING = "expiring" EXPIRED = "expired" EXITED = "finished" @final class Timeout: """Asynchronous context manager for cancelling overdue coroutines. Use `timeout()` or `timeout_at()` rather than instantiating this class directly. """ def __init__(self, when: Optional[float]) -> None: """Schedule a timeout that will trigger at a given loop time. - If `when` is `None`, the timeout will never trigger. - If `when < loop.time()`, the timeout will trigger on the next iteration of the event loop. """ self._state = _State.CREATED self._timeout_handler: Optional[events.TimerHandle] = None self._task: Optional[tasks.Task] = None self._when = when def when(self) -> Optional[float]: """Return the current deadline.""" return self._when def reschedule(self, when: Optional[float]) -> None: """Reschedule the timeout.""" if self._state is not _State.ENTERED: if self._state is _State.CREATED: raise RuntimeError("Timeout has not been entered") raise RuntimeError( f"Cannot change state of {self._state.value} Timeout", ) self._when = when if self._timeout_handler is not None: self._timeout_handler.cancel() if when is None: self._timeout_handler = None else: loop = events.get_running_loop() if when <= loop.time(): self._timeout_handler = loop.call_soon(self._on_timeout) else: self._timeout_handler = loop.call_at(when, self._on_timeout) def expired(self) -> bool: """Is timeout expired during execution?""" return self._state in (_State.EXPIRING, _State.EXPIRED) def __repr__(self) -> str: info = [''] if self._state is _State.ENTERED: when = round(self._when, 3) if self._when is not None else None info.append(f"when={when}") info_str = ' '.join(info) return f"" async def __aenter__(self) -> "Timeout": if self._state is not _State.CREATED: raise RuntimeError("Timeout has already been entered") task = tasks.current_task() if task is None: raise RuntimeError("Timeout should be used inside a task") self._state = _State.ENTERED self._task = task self._cancelling = self._task.cancelling() self.reschedule(self._when) return self async def __aexit__( self, exc_type: Optional[Type[BaseException]], exc_val: Optional[BaseException], exc_tb: Optional[TracebackType], ) -> Optional[bool]: assert self._state in (_State.ENTERED, _State.EXPIRING) if self._timeout_handler is not None: self._timeout_handler.cancel() self._timeout_handler = None if self._state is _State.EXPIRING: self._state = _State.EXPIRED if self._task.uncancel() <= self._cancelling and exc_type is exceptions.CancelledError: # Since there are no new cancel requests, we're # handling this. raise TimeoutError from exc_val elif self._state is _State.ENTERED: self._state = _State.EXITED return None def _on_timeout(self) -> None: assert self._state is _State.ENTERED self._task.cancel() self._state = _State.EXPIRING # drop the reference early self._timeout_handler = None def timeout(delay: Optional[float]) -> Timeout: """Timeout async context manager. Useful in cases when you want to apply timeout logic around block of code or in cases when asyncio.wait_for is not suitable. For example: >>> async with asyncio.timeout(10): # 10 seconds timeout ... await long_running_task() delay - value in seconds or None to disable timeout logic long_running_task() is interrupted by raising asyncio.CancelledError, the top-most affected timeout() context manager converts CancelledError into TimeoutError. """ loop = events.get_running_loop() return Timeout(loop.time() + delay if delay is not None else None) def timeout_at(when: Optional[float]) -> Timeout: """Schedule the timeout at absolute time. Like timeout() but argument gives absolute time in the same clock system as loop.time(). Please note: it is not POSIX time but a time with undefined starting base, e.g. the time of the system power on. >>> async with asyncio.timeout_at(loop.time() + 10): ... await long_running_task() when - a deadline when timeout occurs or None to disable timeout logic long_running_task() is interrupted by raising asyncio.CancelledError, the top-most affected timeout() context manager converts CancelledError into TimeoutError. """ return Timeout(when)