Yes what you're doing looks right except for the "utime.sleep_ms" -- you cannot use a blocking sleep in asyncio code. But you don't need it, the "await asyncio.sleep_ms" should be all you need.

So your diagram says you have three tasks that do something every 5, 5, and 2 seconds respectively.

This program does what I'd expect (see output below). However the thing to remember is that if whatever the "todo()" task is is blocking, then it will prevent other tasks from running, so todo() itself likely needs to be async. Also if todo() takes some time to run, then the sleep time probably needs to be adjusted.

Code: Select all

```
import time
import uasyncio as asyncio
start_ms = time.ticks_ms()
async def task1():
while True:
print(time.ticks_diff(time.ticks_ms(), start_ms), "task 1")
await asyncio.sleep_ms(5000)
async def task2():
while True:
print(time.ticks_diff(time.ticks_ms(), start_ms), "task 2")
await asyncio.sleep_ms(5000)
async def task3():
while True:
print(time.ticks_diff(time.ticks_ms(), start_ms), "task 3")
await asyncio.sleep_ms(2000)
loop = asyncio.get_event_loop()
loop.create_task(task1())
loop.create_task(task2())
loop.create_task(task3())
loop.run_forever()
```

Output:

Code: Select all

```
0 task 1
0 task 2
0 task 3
2002 task 3
4005 task 3
5001 task 1
5001 task 2
6006 task 3
8007 task 3
10003 task 1
10003 task 2
10007 task 3
12009 task 3
14011 task 3
15003 task 1
15003 task 2
16012 task 3
18015 task 3
20003 task 1
```