QUERY · ISSUE
extmod/uasyncio Please provide CPython's synchronisation primitives
extmod
CPython provides Queue, Semaphore, BoundedSemaphore and Condition in addition to the provided Lock and Event classes.
In the case of Queue the issue was raised in https://github.com/micropython/micropython/issues/5828.
CANDIDATE · PULL REQUEST
uasyncio submodule for synchronization primitives, Lock implemented.
@dpgeorge , @peterhinch : Please reviews. Points to consider:
- Name of the submodule. I wished to use uasyncio.sync for qstr reuse, but that may be not clear enough.
- Correctness of implementation. Note that this is compatible with CPython Lock API, which asyncio liking to confusingly introduce non-coroutines methods here and there, .release() being such. So, that was worked around by putting extra yield into acquire(), and even works without scheduling artifacts with the provided example, though in general, it might lead to such.
Looks good to me.
Why not support use in an asynchronous context manager as per this attempt?
Because context manager is a syntactic sugar requiring more code, and uasyncio's goal is to provide required functionality with the minimum of code.
Ok, I'm still not sure about "synchro" part, but there're more things in queue, so merging this. As I figure noone really tested this, I'm leaving debug statements in the code.
To clarify, this was developed to have an example on non-I/O-waiting coroutine which is fully suspended from the main scheduling queue (to be able to test cancellation algorithm and make sure that the proposed solution, which required patching the language core, is fully general).