Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Python

asyncio.sleep

Suspends the current task for a delay and optionally supplies a result when it resumes.

Version: Python 3.14; repository image, production runtime not attested

Syntax

await asyncio.sleep(delay, result=None)

Suspends the current task for a delay and optionally supplies a result when it resumes.

A zero delay yields execution to the event loop. It provides cooperative scheduling without blocking the thread.

Suspend the current task for the delay in seconds so the event loop can run other work. Zero is an explicit cooperative yield. On normal resumption the result argument is returned, defaulting to None. Since 3.13 a NaN delay raises ValueError. In the example a scheduled sibling appends run and the second sleep returns 7.

import asyncio
async def main():
    assert await asyncio.sleep(0, result="ok") == "ok"
    try:
        await asyncio.sleep(float("nan"))
    except ValueError:
        pass
    else:
        raise AssertionError
asyncio.run(main())

Examples

import asyncio

async def main():
    steps = []
    async def mark():
        steps.append("run")
    task = asyncio.create_task(mark())
    await asyncio.sleep(0)
    print(steps)
    print(await asyncio.sleep(0, result=7))
    await task

asyncio.run(main())

Result

['run']
7

Suspend the current task for the delay in seconds so the event loop can run other work. Zero is an explicit cooperative yield. On normal resumption the result argument is returned, defaulting to None. Since 3.13 a NaN delay raises ValueError. In the example a scheduled sibling appends run and the second sleep returns 7.

Sources