Skip to content

Commit d94d2d2

Browse files
committed
Add async/await syntax to FULL_BUILD
This adds the `async def` and `await` verbs to valid CircuitPython syntax using the Micropython implementation. Consider: ``` >>> class Awaitable: ... def __iter__(self): ... for i in range(3): ... print('awaiting', i) ... yield ... return 42 ... >>> async def wait_for_it(): ... a = Awaitable() ... result = await a ... return result ... >>> task = wait_for_it() >>> next(task) awaiting 0 >>> next(task) awaiting 1 >>> next(task) awaiting 2 >>> next(task) Traceback (most recent call last): File "<stdin>", line 1, in <module> StopIteration: 42 >>> ``` and more excitingly: ``` >>> async def it_awaits_a_subtask(): ... value = await wait_for_it() ... print('twice as good', value * 2) ... >>> task = it_awaits_a_subtask() >>> next(task) awaiting 0 >>> next(task) awaiting 1 >>> next(task) awaiting 2 >>> next(task) twice as good 84 Traceback (most recent call last): File "<stdin>", line 1, in <module> StopIteration: ``` Note that this is just syntax plumbing, not an all-encompassing implementation of an asynchronous task scheduler or asynchronous hardware apis. uasyncio might be a good module to bring in, or something else - but the standard Python syntax does not _strictly require_ deeper hardware support. Micropython implements the await verb via the __iter__ function rather than __await__. It's okay. The syntax being present will enable users to write clean and expressive multi-step state machines that are written serially and interleaved according to the rules provided by those users. Given that this does not include an all-encompassing C scheduler, this is expected to be an advanced functionality until the community settles on the future of deep hardware support for async/await in CircuitPython. Users will implement yield-based schedulers and tasks wrapping synchronous hardware APIs with polling to avoid blocking, while their application business logic gets simple `await` statements.
1 parent c7d87ce commit d94d2d2

File tree

2 files changed

+3
-1
lines changed

2 files changed

+3
-1
lines changed

py/circuitpy_mpconfig.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@
7979

8080
#define MICROPY_PY_ARRAY (1)
8181
#define MICROPY_PY_ARRAY_SLICE_ASSIGN (1)
82-
#define MICROPY_PY_ASYNC_AWAIT (0)
8382
#define MICROPY_PY_ATTRTUPLE (1)
8483

8584
#define MICROPY_PY_BUILTINS_BYTEARRAY (1)

py/circuitpy_mpconfig.mk

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,3 +290,6 @@ CFLAGS += -DCIRCUITPY_WIFI=$(CIRCUITPY_WIFI)
290290
# Enabled micropython.native decorator (experimental)
291291
CIRCUITPY_ENABLE_MPY_NATIVE ?= 0
292292
CFLAGS += -DCIRCUITPY_ENABLE_MPY_NATIVE=$(CIRCUITPY_ENABLE_MPY_NATIVE)
293+
294+
# async/await language keyword support
295+
MICROPY_PY_ASYNC_AWAIT ?= $(CIRCUITPY_FULL_BUILD)

0 commit comments

Comments
 (0)