Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Python

asyncio.Event

Shares a Boolean signal between coroutines; waiting tasks resume when the signal is set.

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

Syntax

asyncio.Event()

Shares a Boolean signal between coroutines; waiting tasks resume when the signal is set.

Clearing resets the signal for future waits. An already set event allows a wait to finish immediately.

An Event starts unset. wait suspends until set wakes all waiters and returns True; additional waits pass immediately while the flag stays set. Repeated set calls do not accumulate permits. clear resets the flag for future waits. The example observes True after setting and False after clearing. Use it between tasks of an event loop.

import asyncio
async def main():
    e = asyncio.Event()
    a, b = (asyncio.create_task(e.wait()) for _ in range(2))
    await asyncio.sleep(0)
    assert not a.done() and not b.done()
    e.set()
    assert await a and await b and e.is_set()
    e.clear()
    assert not e.is_set()
asyncio.run(main())

Examples

import asyncio

async def main():
    event = asyncio.Event()
    event.set()
    print(await event.wait(), event.is_set())
    event.clear()
    print(event.is_set())

asyncio.run(main())

Result

True True
False

An Event starts unset. wait suspends until set wakes all waiters and returns True; additional waits pass immediately while the flag stays set. Repeated set calls do not accumulate permits. clear resets the flag for future waits. The example observes True after setting and False after clearing. Use it between tasks of an event loop.

Sources