CKTIS / Python
asyncio.Event
Señala la disponibilidad a varias tareas del bucle mediante una marca inicialmente desactivada.
Versión: Python 3.14; repository image, production runtime not attested
Sintaxis
asyncio.Event()Señala la disponibilidad a varias tareas del bucle mediante una marca inicialmente desactivada.
set despierta a todas las que esperan y wait devuelve True de inmediato mientras la marca siga activa. Repetir set no acumula permisos.
Después de clear, is_set da False y el siguiente wait vuelve a esperar.
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())
Ejemplos
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())
Resultado
True True False
Después de clear, is_set da False y el siguiente wait vuelve a esperar.