kopia lustrzana https://github.com/peterhinch/micropython-samples
uasyncio: add test_can.py.
rodzic
0916a421e0
commit
9a5fd628cc
|
@ -59,6 +59,8 @@ Hopefully these are self-documenting on import.
|
|||
I/O scheduling. Runs on Pyboard.
|
||||
3. `ms_timer.py` and `ms_timer_test.py` A practical use of priority scheduling to
|
||||
implement a timer with higher precision than `asyncio.sleep_ms`. Runs on Pyboard.
|
||||
4. `test_can.py` Demonstrates differences in behaviour between CPython 3.8 and
|
||||
MicroPython. See code comments.
|
||||
|
||||
# 5. CPython compatibility of user primitives
|
||||
|
||||
|
|
|
@ -0,0 +1,62 @@
|
|||
# Shows that MicroPython seems to cancel a task earlier than CPython
|
||||
# Also demonstrates that CPython cancels tasks when run() terminates.
|
||||
try:
|
||||
import asyncio
|
||||
except ImportError:
|
||||
import uasyncio as asyncio
|
||||
|
||||
async def foo(n):
|
||||
try:
|
||||
while True:
|
||||
await asyncio.sleep(0)
|
||||
print(n)
|
||||
except asyncio.CancelledError:
|
||||
print('Task {} canned.'.format(n))
|
||||
raise
|
||||
|
||||
async def main(n):
|
||||
tasks = []
|
||||
for n in range(3):
|
||||
tasks.append(asyncio.create_task(foo(n)))
|
||||
for _ in range(n):
|
||||
await asyncio.sleep(0)
|
||||
print('Cancelling task 1')
|
||||
tasks[1].cancel()
|
||||
for _ in range(3):
|
||||
await asyncio.sleep(0)
|
||||
|
||||
asyncio.run(main(n=3))
|
||||
|
||||
# CPython 3.8
|
||||
#>>> import test_can
|
||||
#0
|
||||
#1
|
||||
#2
|
||||
#Cancelling task 1
|
||||
#0
|
||||
#Task 1 canned.
|
||||
#2
|
||||
#0
|
||||
#2
|
||||
#0
|
||||
#2
|
||||
#0
|
||||
#2
|
||||
#Task 0 canned.
|
||||
#Task 2 canned.
|
||||
#>>>
|
||||
|
||||
# MicroPython
|
||||
#>>> import test_can
|
||||
#0
|
||||
#1
|
||||
#2
|
||||
#Cancelling task 1
|
||||
#Task 1 canned.
|
||||
#0
|
||||
#2
|
||||
#0
|
||||
#2
|
||||
#0
|
||||
#2
|
||||
#>>>
|
Ładowanie…
Reference in New Issue