Coroutine
Definition
Coroutines are a more generalized form of subroutines. Subroutines are entered at one point and exited at another point. Coroutines can be entered, exited, and resumed at many different points. They can be implemented with the async def statement. See also PEP 492.
Baby toy on top of generators
def coroutine(num):
for i in range(1, num):
from_user = yield i
print(f'Send from user to generator {from_user}')
def from_users():
c = coroutine(10)
from_generator = c.send(None)
print(f'From primed generator to base user {from_generator}')
try:
while True:
from_generator = c.send("A")
print(f'From generator to user {from_generator}')
except StopIteration:
print("reaching the end")
Expected output after calling from_user()
The control of the program is getting passed back and forth between these 2 functions. This is pretty much a baby version of asyncio.
Basic async and await
definitions
coroutine - a function with
async defawaitable - any object with
__await__method (i.e.asyncio.Future,asyncio.Task)task - a wrapper around coroutine and gets scheduled by the event loop independently
event loop - scheduler
References
Real Python - Async IO in Python
Hynek Schlawack - https://hynek.me/articles/waiting-in-asyncio/
Python 3.9 docs - Coroutines and Tasks
PEP 492 - Coroutines with async and await syntax
Python 3.9 docs - Asynchronous I/O
Medium - Coroutines in pure Java
Wikipedia - Coroutine
Last updated