Tried to get some understanding of the implementation of the asyncio module. After reading some parts of the code of events.py I noticed the code of the lines get_running_loop python implementation. Among other things, this code says, # NOTE: this function is implemented in C (see _asynciomodule.c) for the functions _get_running_loop
and get_running_loop
. After reading on, I discovered at the bottom of events.py some alias to the pure Python implementations for testing purposes. Getting curious I tried the following script below:
import asyncio
async def main():
print(type(asyncio.events._py__get_running_loop))
# print(type(asyncio.events._get_running_loop))
loop = asyncio.events._py__get_running_loop()
# loop = asyncio.events._get_running_loop()
print(loop)
print(type(loop))
asyncio.run(main(), debug=True)
Running the code above I get:
<class 'builtin_function_or_method'>
<_UnixSelectorEventLoop running=True closed=False debug=True>
<class 'asyncio.unix_events._UnixSelectorEventLoop'>
and after commenting out the C implementation and testing the pure Python implementation I get:
<class 'function'>
None
<class 'NoneType'>
I did expect this result, but I don’t really understand the rationale behind this pure-Python implementation.
So, my question is: What was the reason to have this pure-Python implementation, and how to use it to get non-trivial results (i.e. an event loop)?
I’m glad at any help. I want to improve, so if this question is not appropriate in this forum → please tell me.