Skip to content

Enable ThreadSafeFlag #25

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed

Conversation

jepler
Copy link
Contributor

@jepler jepler commented Aug 6, 2022

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:

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

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

async def aget(scanner):
    await scanner.events.wait()
    return scanner.events.get()

so now the only slightly awkward await aget(kb) is needed.

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.
@tannewt
Copy link
Member

tannewt commented Aug 8, 2022

Why not put threadsafe flag in its own module? That way asyncio stays CPython-compatible.

@dhalbert
Copy link
Contributor

dhalbert commented Aug 8, 2022

Why not put threadsafe flag in its own module? That way asyncio stays CPython-compatible.

Related to #3. Also, I had an idea for an ancillary library:
From #7 (comment):

So any TaskGroup we add will not be quite compatible, and we probably want to add it in an ancillary library (adafruit_asyncio or asynciox or xasyncio or ??).

@jepler jepler closed this Aug 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants