On this page
asyncio.sleep
Prev: asyncio.shield | Up: asyncio | Next: asyncio.start_server
Rel: asyncio.AbstractEventLoop, asyncio.AbstractServer, asyncio.all_tasks, asyncio.as_completed
Ref: docs.python.org — asyncio.sleep
Tags: #public #python3 #stdlib #asyncio
Import
import asyncio
from asyncio import sleep
Signature & Overview
asyncio.sleep(delay, result=None)
Coroutine that completes after a given time (in seconds).
Usage
import asyncio
async def main():
val = await asyncio.sleep(0, result="yielded")
assert val == "yielded"
asyncio.run(main())
Gotchas
await asyncio.sleep(0)yields control to the event loop: Useful for letting other ready tasks run during CPU-heavy loops.- Never call
time.sleep()in async code:time.sleep()blocks the entire event loop thread.
REPL Inspect
from asyncio import sleep
help(sleep)
Linked from
Related notes
- tuts/python3/stdlib_module/asyncio/shield Linked here; links to this page; shared tags: #asyncio, #stdlib
- tuts/python3/stdlib_module/asyncio/start_server Linked here; links to this page; shared tags: #asyncio, #stdlib
- tuts/python3/stdlib_module/asyncio Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/asyncio/AbstractEventLoop Linked here; shared tags: #asyncio, #stdlib
- tuts/python3/stdlib_module/asyncio/AbstractServer Linked here; shared tags: #asyncio, #stdlib
- tuts/python3/stdlib_module/asyncio/all_tasks Linked here; shared tags: #asyncio, #stdlib
- tuts/python3/stdlib_module/asyncio/as_completed Linked here; shared tags: #asyncio, #stdlib
- tuts/python3/stdlib_module/asyncio/Barrier shared tags: #asyncio, #stdlib
- tuts/python3/stdlib_module/asyncio/BaseEventLoop shared tags: #asyncio, #stdlib
- tuts/python3/stdlib_module/asyncio/BaseProtocol shared tags: #asyncio, #stdlib