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 i
(delay: float | None)
| 141 | |
| 142 | |
| 143 | def timeout(delay: float | None) -> Timeout: |
| 144 | """Timeout async context manager. |
| 145 | |
| 146 | Useful in cases when you want to apply timeout logic around block |
| 147 | of code or in cases when asyncio.wait_for is not suitable. For example: |
| 148 | |
| 149 | >>> async with asyncio.timeout(10): # 10 seconds timeout |
| 150 | ... await long_running_task() |
| 151 | |
| 152 | |
| 153 | delay - value in seconds or None to disable timeout logic |
| 154 | |
| 155 | long_running_task() is interrupted by raising asyncio.CancelledError, |
| 156 | the top-most affected timeout() context manager converts CancelledError |
| 157 | into TimeoutError. |
| 158 | """ |
| 159 | loop = events.get_running_loop() |
| 160 | return Timeout(loop.time() + delay if delay is not None else None) |
| 161 | |
| 162 | |
| 163 | def timeout_at(when: float | None) -> Timeout: |