63b9944382
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.
27 lines
590 B
Python
27 lines
590 B
Python
# MicroPython uasyncio module
|
|
# MIT license; Copyright (c) 2019 Damien P. George
|
|
|
|
from .core import *
|
|
|
|
__version__ = (3, 0, 0)
|
|
|
|
_attrs = {
|
|
"wait_for": "funcs",
|
|
"gather": "funcs",
|
|
"Event": "event",
|
|
"Lock": "lock",
|
|
"open_connection": "stream",
|
|
"start_server": "stream",
|
|
}
|
|
|
|
# Lazy loader, effectively does:
|
|
# global attr
|
|
# from .mod import attr
|
|
def __getattr__(attr):
|
|
mod = _attrs.get(attr, None)
|
|
if mod is None:
|
|
raise AttributeError(attr)
|
|
value = getattr(__import__(mod, None, None, True, 1), attr)
|
|
globals()[attr] = value
|
|
return value
|