diff options
author | Damien George <damien.p.george@gmail.com> | 2019-11-13 21:07:58 +1100 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2020-03-26 01:25:45 +1100 |
commit | 63b99443820f53afbdab5201044629d2bfecd73b (patch) | |
tree | cdb041466512e8f97bbb8739f6ec232140b225ad /extmod/uasyncio/funcs.py | |
parent | f05ae416ff61c9381e467e1bf2558ed005b6cc3a (diff) | |
download | micropython-63b99443820f53afbdab5201044629d2bfecd73b.tar.gz micropython-63b99443820f53afbdab5201044629d2bfecd73b.zip |
extmod/uasyncio: Add new implementation of uasyncio module.
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.
Diffstat (limited to 'extmod/uasyncio/funcs.py')
-rw-r--r-- | extmod/uasyncio/funcs.py | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/extmod/uasyncio/funcs.py b/extmod/uasyncio/funcs.py new file mode 100644 index 0000000000..7a4bddf256 --- /dev/null +++ b/extmod/uasyncio/funcs.py @@ -0,0 +1,50 @@ +# MicroPython uasyncio module +# MIT license; Copyright (c) 2019-2020 Damien P. George + +from . import core + + +async def wait_for(aw, timeout): + aw = core._promote_to_task(aw) + if timeout is None: + return await aw + + def cancel(aw, timeout): + await core.sleep(timeout) + aw.cancel() + + cancel_task = core.create_task(cancel(aw, timeout)) + try: + ret = await aw + except core.CancelledError: + # Ignore CancelledError from aw, it's probably due to timeout + pass + finally: + # Cancel the "cancel" task if it's still active (optimisation instead of cancel_task.cancel()) + if cancel_task.coro is not None: + core._task_queue.remove(cancel_task) + if cancel_task.coro is None: + # Cancel task ran to completion, ie there was a timeout + raise core.TimeoutError + return ret + + +async def gather(*aws, return_exceptions=False): + ts = [core._promote_to_task(aw) for aw in aws] + for i in range(len(ts)): + try: + # TODO handle cancel of gather itself + # if ts[i].coro: + # iter(ts[i]).waiting.push_head(cur_task) + # try: + # yield + # except CancelledError as er: + # # cancel all waiting tasks + # raise er + ts[i] = await ts[i] + except Exception as er: + if return_exceptions: + ts[i] = er + else: + raise er + return ts |