I'm working on some networking code in Python that uses the asyncio module and I like to use the Python REPL from -m asyncio a lot for testing examples. What I've noticed though is the default event loop type for the asyncio REPL changes depending on the OS. For example, on Linux it uses the Selector Event Loop while on Windows it uses Proactor.
My question is this:
- Is there any way to specify what event loop asyncio REPL uses (without having to patch the module yourself which works obviously.)
- Follow up question -- if there isn't -- is there perhaps a way to replace a running event loop in a thread with another type of event loop?
Let me know what your thoughts are.
Yes, you're correct. The default event loop used by the asyncio module can depend on the operating system. The default is typically chosen to be the most appropriate for the platform.
On Linux, the default event loop is usually the SelectorEventLoop, and on Windows, it is the ProactorEventLoop. This is because the underlying I/O models are different on these platforms. SelectorEventLoop is based on the select system call, which is well-suited for Linux, while ProactorEventLoop is based on the Windows Proactor I/O model.
If you want to explicitly set the event loop type, you can do so in your code. For example, if you always want to use the SelectorEventLoop, you can set it explicitly:
Or, if you want to check which event loop is currently being used, you can do:
On Linux by default this will print
todo note: Anyone on a Windows platform please edit and add the Windows output