I have this code:
async def foo(x):yield xyield x + 1async def intermediary(y):await foo(y)def bar():c = intermediary(5)
What do I put in bar to get the 5 and the 6 out of c
?
I'm asking because the asyncio
library seems like a lot of magic. And I want to know exactly how the magic works.
Maybe I want to write my own functions that call read
or write
and then inform some top level loop that I wrote that they're waiting for the file descriptor to become readable or writeable.
And then, maybe I want that top level loop to be able to resume my read and write functions (and the whole intermediate chain between the top level loop and them) once those conditions become true.
I already know how to use asyncio
more or less. I wrote this little demo program that computes squares after a delay but launches lots of those tasks that each append to a list after a random interval. It's kind of clumsily written, but it works.
I want to know exactly what that program is doing under the hood. And in order to do that, I have to know how await on that sleep informs the top-level event loop that it wants to sleep (and be called again) for a bit and how the state of all the intermediate stack frames between the call to sleep and the top level event loop are frozen in place then reactivated when the delay is over.