summaryrefslogtreecommitdiff
path: root/extmod/uasyncio
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-06-08all: Replace all uses of umodule in Python code.Jim Mussared
Applies to drivers/examples/extmod/port-modules/tools. 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`.
2023-02-02top: Update Python formatting to black "2023 stable style".Jim Mussared
See https://black.readthedocs.io/en/stable/the_black_code_style/index.html Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
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-09-05all: Update all manifest.py files to use new features.Jim Mussared
Changes in this commit: - Manifest include's now use the directory path where possible (no longer necessary to include the manifest.py file explicitly). - Add manifest.py for all drivers and components that are referenced by port/board manifests. - Replace all uses of freeze() with package()/module(), except for port and board modules. - Use opt=3 everywhere, for consistency and to reduce code size. - Use require() instead of include() for all micropython-lib references. - Remove support for optional board-level manifest.py in mimxrt port, to make it behave the same as other ports (the board must set FROZEN_MANIFEST to a custom manifest.py, which can optionally include the default, port-level manifest). - Also reinstates modules that were accidentally removed from the esp8266 512k build in fbe9417b90474dd1a08749b3a79311a8007a98fb. Signed-off-by: Jim Mussared <jim.mussared@gmail.com> Signed-off-by: Damien George <damien@micropython.org>
2022-08-12extmod/uasyncio: Rename internal _flag to state, to save a qstr.Damien George
Saves about 16 bytes of flash when uasyncio is frozen in. Signed-off-by: Damien George <damien@micropython.org>
2022-08-12extmod/uasyncio: Add clear method to ThreadSafeFlag.Ned Konz
This is useful in situations where the ThreadSafeFlag is reused and needs to be cleared of any previous, unwanted event. For example, clear the flag at the start of an operation, trigger the operation (eg an I2C write), then (a)wait for an external event to set the flag (eg a pin IRQ). Further events may trigger the flag again but these are unwanted and should be cleared before the next cycle starts.
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-24extmod/uasyncio: Implement stream read(-1) to read all data up to EOF.Damien George
Fixes issue #6355. Signed-off-by: Damien George <damien@micropython.org>
2022-06-24extmod/uasyncio: Attempt to write immediately in Stream.write method.Thorsten von Eicken
The main aim of this change is to reduce the number of heap allocations when writing data to a stream. This is done in two ways: 1. Eliminate appending of data when .write() is called multiple times before calling .drain(). With this commit, the data is written out immediately if the underlying stream is not blocked, so there is no accumulation of the data in a temporary buffer. 2. Eliminate copying of non-bytes objects passed to .write(). Prior to this commit, passing a bytearray or memoryview to .write() would always result in a copy of it being made and turned into a bytes object. That won't happen now if the underlying stream is not blocked. Also, this change makes .write () more closely implement the CPython documented semantics: "The method attempts to write the data to the underlying socket immediately. If that fails, the data is queued in an internal write buffer until it can be sent."
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-04-21extmod/uasyncio: Make Python Task match C version with use of asserts.Damien George
This helps to catch bugs when a Task is put on more than one pairing heap. 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>
2022-03-30extmod/uasyncio: Allow task state to be a callable.Damien George
This implements a form of CPython's "add_done_callback()", but at this stage it is a hidden feature and only intended to be used internally. Signed-off-by: Damien George <damien@micropython.org>
2021-12-09all: Update Python formatting to latest Black version 21.12b0.Jim Mussared
Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2021-11-17extmod/uasyncio: Fix gather returning exceptions from a cancelled task.Damien George
Fixes issue #5882.
2021-07-31extmod/uasyncio: In open_connection use address info in socket creation.oclyke
Rudimentary support for various address families. Signed-off-by: oclyke <oclyke@gmail.com>
2021-06-26extmod/uasyncio: Get addr and bind server socket before creating task.Damien George
Currently when using uasyncio.start_server() the socket configuration is done inside a uasyncio.create_task() background function. If the address and port are already in use however this throws an OSError which cannot be cleanly caught behind the create_task(). This commit moves the getaddrinfo and socket binding to the start_server() function, and only creates the task if that succeeds. This means that any OSError from the initial socket configuration is propagated directly up the call stack, compatible with CPython behaviour. See #7444. Signed-off-by: Damien George <damien@micropython.org>
2021-06-16extmod/uasyncio: Fix race with cancelled task waiting on finished task.Damien George
This commit fixes a problem with a race between cancellation of task A and completion of task B, when A waits on B. If task B completes just before task A is cancelled then the cancellation of A does not work. Instead, the CancelledError meant to cancel A gets passed through to B (that's expected behaviour) but B handles it as a "Task exception wasn't retrieved" scenario, printing out such a message (this is because finished tasks point their "coro" attribute to themselves to indicate they are done, and implement the throw() method, but that method inadvertently catches the CancelledError). The correct behaviour is for B to bounce that CancelledError back out. This bug is mainly seen when wait_for() is used, and in that context the symptoms are: - occurs when using wait_for(T, S), if the task T being waited on finishes at exactly the same time as the wait-for timeout S expires - task T will have run to completion - the "Task exception wasn't retrieved message" is printed with "<class 'CancelledError'>" as the error (ie no traceback) - the wait_for(T, S) call never returns (it's never put back on the uasyncio run queue) and all tasks waiting on this are blocked forever from running - uasyncio otherwise continues to function and other tasks continue to be scheduled as normal The fix here reworks the "waiting" attribute of Task to be called "state" and uses it to indicate whether a task is: running and not awaited on, running and awaited on, finished and not awaited on, or finished and awaited on. This means the task does not need to point "coro" to itself to indicate finished, and also allows removal of the throw() method. A benefit of this is that "Task exception wasn't retrieved" messages can go back to being able to print the name of the coroutine function. Fixes issue #7386. Signed-off-by: Damien George <damien@micropython.org>
2021-06-15extmod/uasyncio: Add readinto() method to Stream class.Mike Teachman
With docs and a multi-test using TCP server/client. This method is a MicroPython extension, although there is discussion of adding it to CPython: https://bugs.python.org/issue41305 Signed-off-by: Mike Teachman <mike.teachman@gmail.com>
2021-06-08extmod/uasyncio: Fix start_server and wait_closed race condition.Miguel Grinberg
This fix prevents server.wait_closed() from raising an AttributeError when trying to access server.task. This can happen if it is called immediately after start_server().
2021-04-23extmod/uasyncio: Use .errno instead of .args[0] for OSError exceptions.Damien George
Signed-off-by: Damien George <damien@micropython.org>
2021-02-16extmod/uasyncio: Add ThreadSafeFlag.Jim Mussared
This is a MicroPython-extension that allows for code running in IRQ (hard or soft) or scheduler context to sequence asyncio code. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2021-02-13extmod/uasyncio: Add asyncio.current_task().Jim Mussared
Matches CPython behavior. Fixes #6686
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: Add Task.done() method.Damien George
This is added because task.coro==None is no longer the way to detect if a task is finished. Providing a (CPython compatible) function for this allows the implementation to be abstracted away. 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-08-22extmod/uasyncio: Truncate negative sleeps to 0.Damien George
Otherwise a task that continuously awaits on a large negative sleep can monopolise the scheduler (because its wake time is always less than everything else in the pairing heap). Signed-off-by: Damien George <damien@micropython.org>
2020-07-25extmod/uasyncio: Add StreamReader.readexactly(n) method.Damien George
It raises on EOFError instead of an IncompleteReadError (which is what CPython does). But the latter is derived from EOFError so code compatible with MicroPython and CPython can be written by catching EOFError (eg see included test). Fixes issue #6156. Signed-off-by: Damien George <damien@micropython.org>
2020-06-10extmod/uasyncio: Add asyncio.wait_for_ms function.Damien George
Fixes issue #6107.
2020-04-14extmod/uasyncio: Change cannot to can't in error message, and test exp.Damien George
Follow up to 8e048d2548867aac743866ca5a4c244b7b5aac09 which missed these.
2020-04-13extmod/uasyncio: Add Loop.new_event_loop method.Damien George
This commit adds Loop.new_event_loop() which is used to reset the singleton event loop. This functionality is put here instead of in Loop.close() to make it possible to write code that is compatible with CPython.
2020-04-04extmod/uasyncio: Add global exception handling methods.Kevin Köck
This commit adds support for global exception handling in uasyncio according to the CPython error handling: https://docs.python.org/3/library/asyncio-eventloop.html#error-handling-api This allows a program to receive exceptions from detached tasks and log them to an appropriate location, instead of them being printed to the REPL. The implementation preallocates a context dictionary so in case of an exception there shouldn't be any RAM allocation. The approach here is compatible with CPython except that in CPython the exception handler is called once the task that threw an uncaught exception is freed, whereas in MicroPython the exception handler is called immediately when the exception is thrown.
2020-04-02extmod/uasyncio: Add StreamReader/StreamWriter as aliases of Stream cls.Damien George
To be compatible with CPython. Fixes issue #5847.
2020-04-02extmod/uasyncio: Add error message to Lock.release's RuntimeError.Kevin Köck
Otherwise it can be hard to understand what the error is if a blank RuntimeError is raised.
2020-04-02extmod/uasyncio: Implement Loop.stop() to stop the event loop.Damien George
2020-04-01extmod/uasyncio: Don't create a Loop instance in get_event_loop().Damien George
The event loop is (for now) just a singleton so make it so that Loop instances are not needed.
2020-03-26extmod/uasyncio: Add manifest.py for freezing uasyncio Py files.Damien George
2020-03-26extmod/uasyncio: Add optional implementation of core uasyncio in C.Damien George
Implements Task and TaskQueue classes in C, using a pairing-heap data structure. Using this reduces RAM use of each Task, and improves overall performance of the uasyncio scheduler.
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.