Base di conoscenzaCentro esami
ITItaliano
Scegli la lingua13

CKTIS / Python

asyncio.Event

Segnala la disponibilità a più task del ciclo tramite un indicatore inizialmente disattivato.

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

Sintassi

asyncio.Event()

Segnala la disponibilità a più task del ciclo tramite un indicatore inizialmente disattivato.

set risveglia tutti quelli in attesa e wait restituisce subito True finché l’indicatore rimane attivo. Ripetere set non accumula permessi.

Dopo clear, is_set dà False e il successivo wait torna ad attendere.

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())

Esempi

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())

Risultato

True True
False

Dopo clear, is_set dà False e il successivo wait torna ad attendere.

Fonti