Python Glossary Русский

Python 3.4+Async programmingfunction

asyncio.sleep()

event loop

Suspends a coroutine for the given time without blocking the event loop. The async counterpart of time.sleep().

Syntax

await asyncio.sleep(delay, result=None)

The classic mistake is leaving time.sleep() inside a coroutine: it freezes the whole thread along with the event loop, so every concurrent task stalls even though the code looks asynchronous. The delay is a lower bound, not a guarantee — you never resume earlier, but how much later depends on how busy the loop is. asyncio.sleep(0) is the idiom for yielding control: no real waiting, just a chance for other ready tasks to run.

Examples

import asyncio
async def task(n):
    await asyncio.sleep(0.1)
    return n * 2
async def main():
    r = await task(5)
    print(r)  # → 10
asyncio.run(main())
# asyncio.sleep(0) — уступить управление без паузы

See also

Open in the full glossarydocs.python.orgReport an inaccuracy