Sujet : Re: Await expressions
De : ram (at) *nospam* zedat.fu-berlin.de (Stefan Ram)
Groupes : comp.lang.pythonDate : 01. Feb 2024, 18:32:58
Autres entêtes
Organisation : Stefan Ram
Message-ID : <future-20240201182111@ram.dialup.fu-berlin.de>
References : 1 2 3
ram@zedat.fu-berlin.de (Stefan Ram) writes:
(Then, it should also be interesting to understand how asyncio
uses "await" to implement asynchronous I/O.)
I have taken "sleep" from asyncio\tasks.py and simplified it a bit:
async def sleep( delay, result=None, *, loop=None ):
loop = events.get_running_loop()
future = loop.create_future()
h = loop.call_later
( delay, futures._set_result_unless_cancelled, future, result )
try: return await future
finally: h.cancel()
So, this is how the control is transferred to the event
loop after an "await sleep"! Initially, the control goes
to "sleep", but this transfers the control to the event loop
(until "sleep" stops waiting for its future in "await future").