CKTIS / Python
asyncio.Event
Assinala a disponibilidade a várias tarefas do ciclo através de um indicador inicialmente desativado.
Versão: Python 3.14; repository image, production runtime not attested
Sintaxe
asyncio.Event()Assinala a disponibilidade a várias tarefas do ciclo através de um indicador inicialmente desativado.
set acorda todas as tarefas em espera e wait devolve logo True enquanto o indicador estiver ativo. Repetir set não acumula permissões.
Após clear, is_set dá False e o próximo wait volta 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())
Exemplos
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
Após clear, is_set dá False e o próximo wait volta a esperar.