2021-02-15 01:28:39 -05:00
|
|
|
# Test Event class
|
|
|
|
|
|
|
|
try:
|
2023-08-22 11:15:46 -04:00
|
|
|
import asyncio
|
2021-02-15 01:28:39 -05:00
|
|
|
except ImportError:
|
|
|
|
print("SKIP")
|
|
|
|
raise SystemExit
|
|
|
|
|
|
|
|
|
|
|
|
import micropython
|
|
|
|
|
|
|
|
try:
|
|
|
|
micropython.schedule
|
2023-10-19 16:42:36 -04:00
|
|
|
# CIRCUITPY-CHANGE: no ThreadSafeFlag
|
2023-08-22 11:15:46 -04:00
|
|
|
asyncio.ThreadSafeFlag
|
2021-02-15 01:28:39 -05:00
|
|
|
except AttributeError:
|
|
|
|
print("SKIP")
|
|
|
|
raise SystemExit
|
|
|
|
|
|
|
|
|
|
|
|
async def task(id, flag):
|
|
|
|
print("task", id)
|
|
|
|
await flag.wait()
|
|
|
|
print("task", id, "done")
|
|
|
|
|
|
|
|
|
|
|
|
def set_from_schedule(flag):
|
|
|
|
flag.set()
|
|
|
|
|
|
|
|
|
|
|
|
async def main():
|
|
|
|
flag = asyncio.ThreadSafeFlag()
|
|
|
|
|
|
|
|
# Set the flag from within the loop.
|
|
|
|
t = asyncio.create_task(task(1, flag))
|
|
|
|
print("yield")
|
|
|
|
await asyncio.sleep(0)
|
|
|
|
print("set event")
|
|
|
|
flag.set()
|
|
|
|
print("yield")
|
|
|
|
await asyncio.sleep(0)
|
|
|
|
print("wait task")
|
|
|
|
await t
|
|
|
|
|
|
|
|
# Set the flag from scheduler context.
|
|
|
|
print("----")
|
|
|
|
t = asyncio.create_task(task(2, flag))
|
|
|
|
print("yield")
|
|
|
|
await asyncio.sleep(0)
|
|
|
|
print("set event")
|
|
|
|
micropython.schedule(set_from_schedule, flag)
|
|
|
|
print("yield")
|
|
|
|
await asyncio.sleep(0)
|
|
|
|
print("wait task")
|
|
|
|
await t
|
|
|
|
|
|
|
|
# Flag already set.
|
|
|
|
print("----")
|
|
|
|
print("set event")
|
|
|
|
flag.set()
|
|
|
|
t = asyncio.create_task(task(3, flag))
|
|
|
|
print("yield")
|
|
|
|
await asyncio.sleep(0)
|
|
|
|
print("wait task")
|
|
|
|
await t
|
|
|
|
|
2021-09-01 13:48:15 -04:00
|
|
|
# Flag set, cleared, and set again.
|
|
|
|
print("----")
|
|
|
|
print("set event")
|
|
|
|
flag.set()
|
|
|
|
print("yield")
|
|
|
|
await asyncio.sleep(0)
|
|
|
|
print("clear event")
|
|
|
|
flag.clear()
|
|
|
|
print("yield")
|
|
|
|
await asyncio.sleep(0)
|
|
|
|
t = asyncio.create_task(task(4, flag))
|
|
|
|
print("yield")
|
|
|
|
await asyncio.sleep(0)
|
|
|
|
print("set event")
|
|
|
|
flag.set()
|
|
|
|
print("yield")
|
|
|
|
await asyncio.sleep(0)
|
|
|
|
print("wait task")
|
|
|
|
await t
|
|
|
|
|
2021-02-15 01:28:39 -05:00
|
|
|
|
|
|
|
asyncio.run(main())
|