summaryrefslogtreecommitdiff
path: root/extmod/uasyncio/funcs.py
AgeCommit message (Collapse)Author
2023-06-19extmod/asyncio: Rename uasyncio to asyncio.Jim Mussared
The asyncio module now has much better CPython compatibility and deserves to be just called "asyncio". This will avoid people having to write `from uasyncio import asyncio`. Renames all files, and updates port manifests to use the new path. Also renames the built-in _uasyncio to _asyncio. This work was funded through GitHub Sponsors. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2023-05-02all: Fix various Python coding inconsistencies found by ruff.Christian Clauss
This fixes: - type-comparison (E721): do not compare types, use isinstance(). - string-dot-format-missing-arguments (F524): .format call is missing argument(s) for placeholder(s): {message}. - f-string-missing-placeholders (F541). - is-literal (F632): Use != to compare constant literals. The last one is fixed by just comparing for truthfulness of `state`.
2022-12-14extmod/uasyncio: Fix syntax of generator functions.Damien Tournoud
The compiler is not picky right now, but these are actually all syntax errors: - await is only valid in an async function - async functions that use yield are actually async generators (a construct not supported by the compiler right now)
2022-07-26extmod/uasyncio: Handle gather with no awaitables.Jim Mussared
This previously resulted in gather() yielding but with no way to be resumed. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2022-06-02extmod/uasyncio: Fix edge case for cancellation of wait_for.Damien George
This fixes the cases where the task being waited on finishes just before or just after the wait_for itself is cancelled. Fixes issue #8717. Signed-off-by: Damien George <damien@micropython.org>
2022-04-22extmod/uasyncio: Rename and merge TaskQueue push/pop methods.Damien George
These are internal names and can be safely renamed without affecting user code. push_sorted() and push_head() are merged into a single push() method, which is already how the C version is implemented. pop_head() is simply renamed to pop(). The changes are: - q.push_sorted(task, t) -> q.push(task, t) - q.push_head(task) -> q.push(task) - q.pop_head() -> q.pop() The shorter names and removal of push_head() leads to a code size reduction of between 40 and 64 bytes on bare-metal targets. Signed-off-by: Damien George <damien@micropython.org>
2022-04-21extmod/uasyncio: Fix bug with task ending just after gather is cancel'd.Damien George
This fixes a bug where the gather is cancelled externally and then one of its sub-tasks (that the gather was waiting on) finishes right between the cancellation being queued and being executed. Signed-off-by: Damien George <damien@micropython.org>
2022-03-30extmod/uasyncio: Fix gather cancelling and handling of exceptions.Damien George
The following fixes are made: - cancelling a gather now cancels all sub-tasks of the gather (previously it would only cancel the first) - if any sub-task of a gather raises an exception then the gather finishes (previously it would only finish if the first sub-task raised) Fixes issues #5798, #7807, #7901. Signed-off-by: Damien George <damien@micropython.org>
2021-11-17extmod/uasyncio: Fix gather returning exceptions from a cancelled task.Damien George
Fixes issue #5882.
2020-12-02extmod/uasyncio: Fix cancellation handling of wait_for.Damien George
This commit switches the roles of the helper task from a cancellation task to a runner task, to get the correct semantics for cancellation of wait_for. Some uasyncio tests are now disabled for the native emitter due to issues with native code generation of generators and yield-from. Fixes #5797. Signed-off-by: Damien George <damien@micropython.org>
2020-12-02extmod/uasyncio: Delay calling Loop.call_exception_handler by 1 loop.Damien George
When a tasks raises an exception which is uncaught, and no other task await's on that task, then an error message is printed (or a user function called) via a call to Loop.call_exception_handler. In CPython this call is made when the Task object is freed (eg via reference counting) because it's at that point that it is known that the exception that was raised will never be handled. MicroPython does not have reference counting and the current behaviour is to deal with uncaught exceptions as early as possible, ie as soon as they terminate the task. But this can be undesirable because in certain cases a task can start and raise an exception immediately (before any await is executed in that task's coro) and before any other task gets a chance to await on it to catch the exception. This commit changes the behaviour so that tasks which end due to an uncaught exception are scheduled one more time for execution, and if they are not await'ed on by the next scheduling loop, then the exception handler is called (eg the exception is printed out). Signed-off-by: Damien George <damien@micropython.org>
2020-06-10extmod/uasyncio: Add asyncio.wait_for_ms function.Damien George
Fixes issue #6107.
2020-03-26extmod/uasyncio: Add new implementation of uasyncio module.Damien George
This commit adds a completely new implementation of the uasyncio module. The aim of this version (compared to the original one in micropython-lib) is to be more compatible with CPython's asyncio module, so that one can more easily write code that runs under both MicroPython and CPython (and reuse CPython asyncio libraries, follow CPython asyncio tutorials, etc). Async code is not easy to write and any knowledge users already have from CPython asyncio should transfer to uasyncio without effort, and vice versa. The implementation here attempts to provide good compatibility with CPython's asyncio while still being "micro" enough to run where MicroPython runs. This follows the general philosophy of MicroPython itself, to make it feel like Python. The main change is to use a Task object for each coroutine. This allows more flexibility to queue tasks in various places, eg the main run loop, tasks waiting on events, locks or other tasks. It no longer requires pre-allocating a fixed queue size for the main run loop. A pairing heap is used to queue Tasks. It's currently implemented in pure Python, separated into components with lazy importing for optional components. In the future parts of this implementation can be moved to C to improve speed and reduce memory usage. But the aim is to maintain a pure-Python version as a reference version.