then schedules the task to run "soon". "Soon" means it will get a turn to run as soon
other existing tasks have given up control.
Then the program uses await asyncio.gather() , which waits for all the tasks it's
passed to finish.
async def main():
animation_controls = AnimationControls()
button_task = asyncio.create_task(monitor_button(button_pin,
animation_controls))
animation_task = asyncio.create_task(rainbow_cycle(animation_controls))
blink_task = asyncio.create_task(blink(animation_controls))
await asyncio.gather(button_task, animation_task, blink_task)
Finally, run the main() function to execute the code within.
asyncio.run(main())
My program ended? What happened?
await.gather(...) runs until all the listed tasks have finished. If gather
completes, that means all the tasks listed have finished.
The most common causes of a task ending are:
an exception occurred causing the task to end
the task function finished
If you want to ensure the task executes forever, have a loop in your task function (e.g.
a while True loop).
The following example is greatly oversimplified, but demonstrates what including a
loop in your task function might look like.
async def never_ending_task():
while True:
print("I'm looping!")
await asyncio.sleep(0)
•
•
©Adafruit Industries Page 188 of 263