October 2018
Beginner to intermediate
466 pages
12h 2m
English
A canonical example of a blocking function is the time.sleep call. Let's use the asynchronous version of this call to illustrate the basics of an AsyncIO event loop, as follows:
import asyncioimport randomasync def random_sleep(counter): delay = random.random() * 5 print("{} sleeps for {:.2f} seconds".format(counter, delay)) await asyncio.sleep(delay) print("{} awakens".format(counter))async def five_sleepers(): print("Creating five tasks") tasks = [asyncio.create_task(random_sleep(i)) for i in range(5)] print("Sleeping after starting five tasks") await asyncio.sleep(2) print("Waking and waiting for five tasks") await asyncio.gather(*tasks)asyncio.get_event_loop().run_until_complete(five_sleepers())print("Done five tasks") ...