From 485869af8a071664857ccaa63806044ac353e108 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Sat, 6 Aug 2022 10:45:30 -0500 Subject: [PATCH] Enable ThreadSafeFlag This didn't work since CircuitPython names the module `io`, not `uio`. This extension to asyncio is not compatible with standard Python. However (like asyncio.sleep_ms), is a useful extension. One thing about it is that the core can use ThreadSafeFlag to enable use like this: ```py async def keyboard_task(pin): with keypad.Keys((pin,), value_when_pressed=False) as kb: while True: await kb.events.wait() print(kb.events.get()) ``` Other changes are needed in the core before this works, and it still feels a bit of a hack. It's too bad you can't just write ```py print(await kb.events.aget()) ``` but I still didn't find a way for the `wait` to return a useful value. A Python-coded wrapper function such as ```py async def aget(scanner): await scanner.events.wait() return scanner.events.get() ``` so now the only slightly awkward `await aget(kb)` is needed. --- asyncio/event.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/asyncio/event.py b/asyncio/event.py index 04f6e15..f26174c 100644 --- a/asyncio/event.py +++ b/asyncio/event.py @@ -71,9 +71,9 @@ async def wait(self): # that asyncio will poll until a flag is set. # Note: Unlike Event, this is self-clearing. try: - import uio + import io - class ThreadSafeFlag(uio.IOBase): + class ThreadSafeFlag(io.IOBase): def __init__(self): self._flag = 0