7.13. AsyncIO Event Loop¶
Async code can only run inside an event loop.
The event loop is the driver code that manages the cooperative multitasking.
You can create multiple threads and run different event loops in each of them.
Python will create a default event loop only in Main Thread
Python will not create an event loop automatically for you on any other than main thread by default, this is to prevent from having multiple event lops created explicitly
Event loop can execute only one callback (coroutine) at a time
Some callbacks (coroutines) can schedule themselves once again (trampoline)
For example, Django uses the main thread to wait for incoming requests, so we can't run an asyncio event loop there, but we can start a separate worker thread for our event loop 3.
An event loop runs in a thread (typically the main thread) and executes all callbacks and Tasks in its thread. While a Task is running in the event loop, no other Tasks can run in the same thread. When a Task executes an await expression, the running Task gets suspended, and the event loop executes the next Task. 4
The ultimate loop implementation for UNIXes (run this on production)
$ pip install uvloop
>>> ... import uvloop ... ... uvloop.install() ... ... loop = asyncio.new_event_loop() ... loop <uvloop.Loop running=False closed=False debug=False>
Kennedy, M. Demystifying Python's Async and Await Keywords. Publisher: JetBrainsTV. Year: 2019. Retrieved: 2022-03-10. URL: https://www.youtube.com/watch?v=F19R_M4Nay4
Langa, Ł. import asyncio: Learn Python's AsyncIO. Year: 2020. Retrieved: 2022-03-10. URL: https://www.youtube.com/playlist?list=PLhNSoGM2ik6SIkVGXWBwerucXjgP1rHmB
Poirier, D. Asyncio (superseded by async page). Year: 2021. Retrieved: 2022-03-17. URL: https://cheat.readthedocs.io/en/latest/python/asyncio.html
Python documentation. Developing with asyncio. Concurrency and Multithreading. Year: 2022. Retrieved: 2022-03-17. URL: https://docs.python.org/3/library/asyncio-dev.html#concurrency-and-multithreading