Guide to uasyncio V3

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: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Guide to uasyncio V3

Post by pythoncoder » Fri Apr 10, 2020 5:30 pm

I have updated my micropython-async repo to provide information on the new uasyncio. This includes:
  • A guide to syntax changes for porting code from V2.
  • A revised tutorial.
  • Code examples.
  • Classes for debouncing switches and pushbuttons with callbacks.
  • A retriggerable timer class (watchdog/monostable type object) with a callback.
  • Synchronisation primitives.
The primitives provide CPython-compatible implementations of Queue, Semaphore, BoundedSemaphore and Condition. They aren't efficient but they will act as stand-ins until Damien releases optimised versions.

The material is a work-in-progress and rough round the edges, but should clarify some of the confusion.
Peter Hinch
Index to my micropython libraries.

stijn
Posts: 735
Joined: Thu Apr 24, 2014 9:13 am

Re: Guide to uasyncio V3

Post by stijn » Fri Apr 10, 2020 5:32 pm

Looking good, and thanks for all the work which went into this!

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

Re: Guide to uasyncio V3

Post by kevinkk525 » Fri Apr 10, 2020 7:28 pm

This is an awesome tutorial!
Thanks a lot for your efforts in porting all examples to the new version and writing that new tutorial!

I haven't read through everything in detail yet, but I found a paragraph that is not completely correct:
5.1 Exceptions

Where an exception occurs in a task, it should be trapped either in that task or in a task which is awaiting its completion. This ensures that the exception is not propagated to the scheduler. If this occurred the scheduler would stop running, passing the exception to the code which started the scheduler. Consequently, to avoid stopping the scheduler, tasks launched with asyncio.create_task() must trap any exceptions internally.
An exception only stops the scheduler if it occurs in the task created by *asyncio.run(task())* because this by definition only runs the scheduler until *task()* is finished.
If an exception occurs in a task created by *asyncio.create_task* then it will be handled by *Loop.default_exception_handler* and not stop the scheduler. Using a custom exception handler you can then log those uncaught exceptions.
Consequently starting the scheduler with *Loop.run_forever()* will result in the scheduler never being stopped (unless there is a bug in the scheduler itself).

Also it is therefore not neccessary to trap all exceptions. Actually the contrary is advisable in my opinion, at least for TimeoutErrors and CancelledErros. Reason: If a coroutine catches a cancellation error without raising it, the coroutine awaiting that task will never know what happened to the task it awaited.
Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode

User avatar
mattyt
Posts: 410
Joined: Mon Jan 23, 2017 6:39 am

Re: Guide to uasyncio V3

Post by mattyt » Sat Apr 11, 2020 1:56 am

Thanks so much Peter! Your asyncio documentation is a wonderful resource and I really appreciate the effort you've invested in a) writing it and b) keeping it up-to-date. Awesome work. :)

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

Re: Guide to uasyncio V3

Post by pythoncoder » Sat Apr 11, 2020 5:13 am

kevinkk525 wrote:
Fri Apr 10, 2020 7:28 pm
...
I haven't read through everything in detail yet, but I found a paragraph that is not completely correct:
...
Thanks for that. I'll update that section. Re interception of timeouts and cancellation, you might know the answer to this issue. The question is whether it is possible for a coro to distinguish between its being cancelled or it timing out. So far I can't see how to do it.
Peter Hinch
Index to my micropython libraries.

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

Re: Guide to uasyncio V3

Post by kevinkk525 » Sat Apr 11, 2020 6:33 am

I answered to the issue on github.
To provide an answer here too:

The explanation is that the coro/task is being cancelled. It doesn't matter why it is being cancelled (due to timeout or task cancellation or whatever reason you may come up with). Task.cancel() is being called and the coro sees a CancelledError.
So it will always see a CancelledError and not a TimeoutError. So you can't distinguish between the coro being cancelled because of Task.cancel() or a wait_for Timeout.

Only the caller of wait_for gets that distinction because only wait_for knows the reason for the cancellation of the task. (Basically not even the scheduler knows, it's just a variable inside wait_for).
Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode

skylin008
Posts: 88
Joined: Wed Mar 11, 2015 6:21 am

Re: Guide to uasyncio V3

Post by skylin008 » Mon Apr 13, 2020 2:36 pm

@pythoncoder Thank you very much for you shared your code.I had been rebuilded the micropython firmware. how to porting to the uasyncio to the firmware.

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

Re: Guide to uasyncio V3

Post by pythoncoder » Mon Apr 13, 2020 2:56 pm

If you build from up to date sources uasyncio will be included in the firmware. Alternatively install the latest daily build.
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: Guide to uasyncio V3

Post by jimmo » Tue Apr 14, 2020 2:23 am

Thank you Peter, this is absolutely wonderful!!

doublevee
Posts: 75
Joined: Mon Jul 02, 2018 11:09 pm

Re: Guide to uasyncio V3

Post by doublevee » Wed Apr 15, 2020 7:17 am

Thanks Peter - a really great resource. Appreciate all the effort you put into this and maintaining it.

Post Reply