Page 1 of 1

New uasyncio feature: ThreadSafeFlag

Posted: Fri Feb 19, 2021 6:00 pm
by pythoncoder
I thought it was worth drawing attention to this as it's a significant enhancement.

Thanks to @jimmo uasyncio has an official thread safe way to interface with code such as interrupt service routines and code running in another thread. This is documented here. I have also updated my tutorial with some usage examples.

My irq_event primitive is now deleted. It did the same thing, using the same I/O stream technique. Just less elegantly coded ;)

Re: New uasyncio feature: ThreadSafeFlag

Posted: Mon Feb 22, 2021 3:54 am
by jimmo
pythoncoder wrote:
Fri Feb 19, 2021 6:00 pm
Thanks to @jimmo uasyncio has an official thread safe way to interface with code such as interrupt service routines and code running in another thread. This is documented here. I have also updated my tutorial with some usage examples.
Thanks Peter! And great to see this in your tutorial already. Sorry this was a long time coming.

The implementation is a bit sub-optimal but we'll need some more architectural changes to improve it, but the API should stay the same. Eventually uevent (or something like it) will allow this to do some neat stuff with low power management.

Worth noting that this is only supported in nightly builds for now, and will be part of the upcoming 1.15 release.

Re: New uasyncio feature: ThreadSafeFlag

Posted: Mon Feb 22, 2021 6:39 am
by pythoncoder
Good point. I've added a note to that effect.

Re: New uasyncio feature: ThreadSafeFlag

Posted: Thu Sep 02, 2021 9:20 am
by agonnen
Hi,
What would be the best approach to set ThreadSafeFlag from C code, possibly from another thread or interrupt context?

Re: New uasyncio feature: ThreadSafeFlag

Posted: Wed Sep 15, 2021 1:21 am
by jimmo
agonnen wrote:
Thu Sep 02, 2021 9:20 am
What would be the best approach to set ThreadSafeFlag from C code, possibly from another thread or interrupt context?
This is an area we're still trying to sort out. See https://github.com/micropython/micropython/pull/6125 and the linked PRs.

Basically the idea is that peripheral drivers (in C or Python) should be able to signal (via uevent, or whatever the mechanism is), and then asyncio is "polling" uevent. This means we can centralise all the event sources and simplify power management etc.

In the short term, the way modbluetooth & aioble does it is the C code raises a regular Python callback, and that calls ThreadSafeFlag::set.