New uasyncio feature: ThreadSafeFlag

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

New uasyncio feature: ThreadSafeFlag

Post by pythoncoder » Fri Feb 19, 2021 6:00 pm

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 ;)
Peter Hinch
Index to my micropython libraries.

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: New uasyncio feature: ThreadSafeFlag

Post by jimmo » Mon Feb 22, 2021 3:54 am

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.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: New uasyncio feature: ThreadSafeFlag

Post by pythoncoder » Mon Feb 22, 2021 6:39 am

Good point. I've added a note to that effect.
Peter Hinch
Index to my micropython libraries.

agonnen
Posts: 27
Joined: Sat Oct 13, 2018 7:52 pm

Re: New uasyncio feature: ThreadSafeFlag

Post by agonnen » Thu Sep 02, 2021 9:20 am

Hi,
What would be the best approach to set ThreadSafeFlag from C code, possibly from another thread or interrupt context?

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: New uasyncio feature: ThreadSafeFlag

Post by jimmo » Wed Sep 15, 2021 1:21 am

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.

Post Reply