Motivating the Use of Asyncio
-
Asyncio is a library used for writing asynchronous code
- Asynchronous code is a form of concurrent code
- Asyncio uses coroutines for asynchronous programming
-
Asynchronous programming has the following properties:
- Asynchronous routines can be paused
- Asynchronous routines can be resumed
- Other routines can be run while routines are paused
-
In the following diagram:
- White terms represent concepts
- Green terms represent implementations of those concepts
Illustrating Async Code with a Chess Analogy
- Asyncio may seem counterintuitive
-
Specifically, how can asyncio
- Use a single thread
- And a single CPU core
- And be concurrent
- Suppose we play in a chess exhibition
- Specifically, we play multiple amateur players
-
There are two ways of conducting the exhibition:
- Synchronously
- Asynchronously
-
In the chess exhibition, let's assume the following:
- There are opponents
- Each move only takes us seconds
- Each move takes seconds for each of our opponents
- There are pair-moves on average ( moves in total)
Synchronously Playing Chess
- We complete one game at a time
- We never play two games at the same time
- Each game takes minutes:
- The entire exhibition takes hours:
Asynchronously Playing Chess
- We move from table to table
- We make one move at each table
- Our oppenent needs time to respond to our move
- They need some time to think
- In the mean time, we move to another opponent and return when they've made their move
- One move across all games takes us minutes
- The entire exhibition is now cut down to hour
The History of async
and await
- 3.3: The
yield from
allows for generator delegation -
3.4: asyncio was introduced
- It was added to the Python standard library
- It was considered very unstable at this time
-
3.5:
async
andawait
were included in the Python grammar- They were used to signify and wait on coroutines
- They were not yet reserved keywords
- For example, custom functions and variables still could be named
async
andawait
-
3.6: Asynchronous generators were introduced
- Asyncio was considered stable
-
3.7:
async
andawait
became reserved keywords- They could not be used as identifiers
- They replaced the
asyncio.coroutine()
decorator asyncio.run()
was introduced to the asyncio package
References
Next