Reputation: 2827
Python can switch contexts when await-ing asyncio.sleep and aiohttp functions. How does python eventually know when a context needs to switch? If I wanted to implement my own IO functions, what APIs do we need to call?
Based on http://www.dabeaz.com/coroutines/Coroutines.pdf, we can use the select api to let context switch when programming with yield. In python 3 with async and await, is it still the only way?
Upvotes: 1
Views: 1424
Reputation: 1437
You are asking questions about the C internals of python. There is no C API currently to access the asyncio loop. If you need to write async C code see this answer:
python - how to implement a C-function as awaitable (coroutine)
we can use the select api to let context switch when programming with yield. In python 3 with async and await, is it still the only way?
You don't need to know the internals to use it. If you want to learn more about the various ways to create an event loop in C see this library:
See the code in src for epoll, kqueue, select.
Also note that you can replace the asyncio event loop with this python module. So you can learn more here as well. Uvloop's event loop uses the C library libuv.
https://github.com/MagicStack/uvloop
If you have more questions about the C code add them as comments below and I can fill in further details.
Upvotes: 5