Python Glossary Русский

Python 3.11+Async programmingfunction

asyncio.timeout_at()

timeouts

Like asyncio.timeout(), but takes an absolute deadline on the event loop clock instead of a duration.

Syntax

async with asyncio.timeout_at(when): ...

The deadline is measured with loop.time(), the loop's monotonic clock, not time.time(): passing a Unix timestamp yields a deadline decades away or one already past. Useful when several steps share one deadline: compute deadline = loop.time() + 5 once and pass it along. Added in 3.11.

Examples

import asyncio
async def main():
    loop = asyncio.get_running_loop()
    deadline = loop.time() + 0.02  # часы цикла, не time.time()
    try:
        async with asyncio.timeout_at(deadline):
            await asyncio.sleep(0.01)
            print('шаг 1')
            await asyncio.sleep(1)
    except TimeoutError:
        print('дедлайн')
asyncio.run(main())  # → шаг 1 / дедлайн

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy