pythoncoder's uasyncio fork

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
Post Reply
User avatar
pythoncoder
Posts: 3068
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

pythoncoder's uasyncio fork

Post by pythoncoder » Sat Jul 07, 2018 11:21 am

I have updated my uasyncio repo. These are the main changes, prompted by the recent addition of io.IOBase.
  • Tutorial now includes details of how to use io.IOBase to write device drivers.
  • Added an "under the hood" doc which attempts to explain how uasyncio works.
  • Added a fast_io version of uasyncio containing some pending changes plus extra comments to aid understanding of uasyncio.
Pending changes in this version:
  • Fixes a bug whereby bidirectional stream devices (such as UARTS) can fail.
  • Throws an exception rather than silently failing if (e.g.) CreateTask is called incorrectly (a common error).
  • Provides an optional facility to schedule I/O at high priority.
The last of these supersedes my asyncio_priority solution. It enables the same capability of running selected devices at high priority, but in a considerably more efficient manner (owing to io.IOBase). It also means that the API is changed in only one place: initialisation of the event loop. This means that applications and drivers are written in standard MicroPython. To switch between official and fast_io version requires no code changes. To use the latter's priority I/O facility requires just one line to be amended.

The ability to write I/O device drivers in Python is a major improvement and eliminates the need for the ad hoc API changes in aysncio_priority. It also provides more efficient polling by means of the select mechanism. Finally stream I/O can be applied to devices not normally considered to be streaming devices: I have included examples.
Peter Hinch

kevinkk525
Posts: 183
Joined: Sat Feb 03, 2018 7:02 pm

Re: uasyncio

Post by kevinkk525 » Sat Jul 07, 2018 2:41 pm

Thanks a lot for you awesome work and tutorials!

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

Re: uasyncio

Post by pythoncoder » Sun Jul 08, 2018 9:39 am

Thank you. My enthusiasm for uasyncio stems from the fact that in 43 (gulp) years of developing firmware most of the nontrivial applications used cooperative multi-tasking. Also from experience of debugging applications using pre-emptive multi tasking. A total can of worms :(
Peter Hinch

cefn
Posts: 205
Joined: Tue Aug 09, 2016 10:58 am

Re: uasyncio

Post by cefn » Mon Jul 09, 2018 10:12 pm

"in 43 (gulp) years of developing firmware most of the nontrivial applications used cooperative multi-tasking"

It's great to have someone with your kind of experience wanting to give back to the community - a level of commitment which deserves real recognition, and passes on insights from so many years of learning-by-doing. Thanks for all your efforts!

shawwwn
Posts: 16
Joined: Tue Feb 06, 2018 5:22 am

Re: uasyncio

Post by shawwwn » Fri Sep 21, 2018 10:38 am

Big thanks for your contribution!

I have been using uasyncio for all my tasks(ESP's network interrupt can be horribly slow).

Didn't bother to read the source code, could you enlighten me about what change in fast_io that makes it fast?

kevinkk525
Posts: 183
Joined: Sat Feb 03, 2018 7:02 pm

Re: uasyncio

Post by kevinkk525 » Fri Sep 21, 2018 11:42 am

You can read the documentation of fast_io. It's explained there.

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

Re: uasyncio

Post by pythoncoder » Fri Sep 21, 2018 11:53 am

Quite. See the docs.
Peter Hinch

shawwwn
Posts: 16
Joined: Tue Feb 06, 2018 5:22 am

Re: uasyncio

Post by shawwwn » Fri Sep 21, 2018 12:49 pm

Correct me if I am wrong: this version adds a separate queue for high priority IO polling while the stream IO has been completely rewritten to accommodate the newly introduced ioctl()?

Also, I noticed in the previous uasyncio implementation, poll.register() was used to poll IO, how does this differ from the select() method described in the fast_io documentation?

Thanks in advance.

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

Re: uasyncio

Post by pythoncoder » Sat Sep 22, 2018 4:20 am

The rewrite is for the reasons stated in the doc (the nine points in the opening para). Fixing the bug mentioned added some complexity; the reasons for change go some way beyond reducing I/O latency. Most of the changes are in __init__.py. Changes to core.py are relatively minor.

The code still uses select.poll, see this line so I think there is a misunderstanding here.

The design is intended to be a "drop in" replacement for official uasyncio. If you have code which runs under official uasynco but have concerns about I/O latency or want to address the timing issues mentioned in the docs, try my version.
Peter Hinch

Post Reply