Python asynchronous:
Async / await
Python is often learned as a declarative / imperative language.
When we want to speed up computation or perform several task in parallel, it becomes unclear how to do as there are many “asynchronous” possibilities.
We will shortly review each of it, describe what it is useful for or not.
asyncio
Note: If you run stuff in a notebook, the output may not pop up correctly.
Concurrency, parallelism, threading, multiprocessing
Coroutines
Concurrency: large domain, where stuff can overlap.
parallelism: Subdomain of concurrency:
(VS sequential)
import asyncio
import numpy as np
async def count():
a = np.random.randint(0, 10)
print("One-{}".format(a))
time.sleep(0.3)
await asyncio.sleep(1)
print("Two-{}".format(a))
async def main():
await asyncio.gather(count(), count(), count())
if __name__ == "__main__":
import time
s = time.perf_counter()
asyncio.run(main())
elapsed = time.perf_counter() - s
print(f"{__file__} executed in {elapsed:0.2f} seconds.")
Behavior:
+”one”+sleep+——————+two +++++++”one”+sleep+——————+two +++++++++++++”one”+sleep+——————+two
Once the first thread reach “sleep”, then it is as if there is nothing else to do, so it let the hand to the next thread.
For the print("Two")
, as the previous thread has finish, there is no conflict.
A function that you introduce with async def is a coroutine. It may use await, return, or yield, but all of these are optional. Declaring async def noop(): pass is valid:
Be careful about:
If there are 9 tasks and 8 workers, then it would take “1+1” task time to compute the overall. So it is not efficient.
What about very high granularity ? Suppose you have 8000 tasks. Is is better to make tasks of one operation, or task with $k$ operations ??
Use a queue. Cut a task into batch of 1, 2, 3, 4, … for the same total amount of computation. Check what is the communication time.
Producer Consummers
http://www.dabeaz.com/coroutines/ https://snarky.ca/how-the-heck-does-async-await-work-in-python-3-5/ https://pymotw.com/3/asyncio/coroutines.html
>> You can subscribe to my mailing list here for a monthly update. <<