2019-11-13 05:07:58 -05:00
|
|
|
# MicroPython uasyncio module
|
2022-06-01 00:52:38 -04:00
|
|
|
# MIT license; Copyright (c) 2019-2022 Damien P. George
|
2019-11-13 05:07:58 -05:00
|
|
|
|
|
|
|
from . import core
|
|
|
|
|
|
|
|
|
2022-06-01 00:52:38 -04:00
|
|
|
def _run(waiter, aw):
|
|
|
|
try:
|
|
|
|
result = await aw
|
|
|
|
status = True
|
|
|
|
except BaseException as er:
|
|
|
|
result = None
|
|
|
|
status = er
|
|
|
|
if waiter.data is None:
|
|
|
|
# The waiter is still waiting, cancel it.
|
|
|
|
if waiter.cancel():
|
|
|
|
# Waiter was cancelled by us, change its CancelledError to an instance of
|
|
|
|
# CancelledError that contains the status and result of waiting on aw.
|
|
|
|
# If the wait_for task subsequently gets cancelled externally then this
|
|
|
|
# instance will be reset to a CancelledError instance without arguments.
|
|
|
|
waiter.data = core.CancelledError(status, result)
|
|
|
|
|
|
|
|
|
2020-06-05 07:26:27 -04:00
|
|
|
async def wait_for(aw, timeout, sleep=core.sleep):
|
2019-11-13 05:07:58 -05:00
|
|
|
aw = core._promote_to_task(aw)
|
|
|
|
if timeout is None:
|
|
|
|
return await aw
|
|
|
|
|
2020-11-30 22:22:16 -05:00
|
|
|
# Run aw in a separate runner task that manages its exceptions.
|
2022-06-01 00:52:38 -04:00
|
|
|
runner_task = core.create_task(_run(core.cur_task, aw))
|
2019-11-13 05:07:58 -05:00
|
|
|
|
|
|
|
try:
|
2020-11-30 22:22:16 -05:00
|
|
|
# Wait for the timeout to elapse.
|
|
|
|
await sleep(timeout)
|
|
|
|
except core.CancelledError as er:
|
2022-06-01 00:52:38 -04:00
|
|
|
status = er.value
|
|
|
|
if status is None:
|
2020-11-30 22:22:16 -05:00
|
|
|
# This wait_for was cancelled externally, so cancel aw and re-raise.
|
|
|
|
runner_task.cancel()
|
|
|
|
raise er
|
2022-06-01 00:52:38 -04:00
|
|
|
elif status is True:
|
|
|
|
# aw completed successfully and cancelled the sleep, so return aw's result.
|
|
|
|
return er.args[1]
|
2020-11-30 22:22:16 -05:00
|
|
|
else:
|
|
|
|
# aw raised an exception, propagate it out to the caller.
|
|
|
|
raise status
|
|
|
|
|
|
|
|
# The sleep finished before aw, so cancel aw and raise TimeoutError.
|
|
|
|
runner_task.cancel()
|
|
|
|
await runner_task
|
|
|
|
raise core.TimeoutError
|
2019-11-13 05:07:58 -05:00
|
|
|
|
|
|
|
|
2020-06-05 07:26:27 -04:00
|
|
|
def wait_for_ms(aw, timeout):
|
|
|
|
return wait_for(aw, timeout, core.sleep_ms)
|
|
|
|
|
|
|
|
|
2022-03-28 21:57:04 -04:00
|
|
|
class _Remove:
|
|
|
|
@staticmethod
|
|
|
|
def remove(t):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2019-11-13 05:07:58 -05:00
|
|
|
async def gather(*aws, return_exceptions=False):
|
2022-07-20 19:54:02 -04:00
|
|
|
if not aws:
|
|
|
|
return []
|
|
|
|
|
2022-03-28 21:57:04 -04:00
|
|
|
def done(t, er):
|
2022-04-20 23:19:34 -04:00
|
|
|
# Sub-task "t" has finished, with exception "er".
|
2022-03-28 21:57:04 -04:00
|
|
|
nonlocal state
|
2022-04-20 23:19:34 -04:00
|
|
|
if gather_task.data is not _Remove:
|
|
|
|
# The main gather task has already been scheduled, so do nothing.
|
|
|
|
# This happens if another sub-task already raised an exception and
|
|
|
|
# woke the main gather task (via this done function), or if the main
|
|
|
|
# gather task was cancelled externally.
|
2022-03-28 21:57:04 -04:00
|
|
|
return
|
|
|
|
elif not return_exceptions and not isinstance(er, StopIteration):
|
|
|
|
# A sub-task raised an exception, indicate that to the gather task.
|
|
|
|
state = er
|
|
|
|
else:
|
|
|
|
state -= 1
|
|
|
|
if state:
|
|
|
|
# Still some sub-tasks running.
|
|
|
|
return
|
|
|
|
# Gather waiting is done, schedule the main gather task.
|
2022-04-20 03:20:07 -04:00
|
|
|
core._task_queue.push(gather_task)
|
2022-03-28 21:57:04 -04:00
|
|
|
|
2019-11-13 05:07:58 -05:00
|
|
|
ts = [core._promote_to_task(aw) for aw in aws]
|
|
|
|
for i in range(len(ts)):
|
2022-03-28 21:57:04 -04:00
|
|
|
if ts[i].state is not True:
|
|
|
|
# Task is not running, gather not currently supported for this case.
|
|
|
|
raise RuntimeError("can't gather")
|
|
|
|
# Register the callback to call when the task is done.
|
|
|
|
ts[i].state = done
|
|
|
|
|
|
|
|
# Set the state for execution of the gather.
|
|
|
|
gather_task = core.cur_task
|
|
|
|
state = len(ts)
|
|
|
|
cancel_all = False
|
|
|
|
|
|
|
|
# Wait for the a sub-task to need attention.
|
|
|
|
gather_task.data = _Remove
|
|
|
|
try:
|
|
|
|
yield
|
|
|
|
except core.CancelledError as er:
|
|
|
|
cancel_all = True
|
|
|
|
state = er
|
|
|
|
|
|
|
|
# Clean up tasks.
|
|
|
|
for i in range(len(ts)):
|
|
|
|
if ts[i].state is done:
|
|
|
|
# Sub-task is still running, deregister the callback and cancel if needed.
|
|
|
|
ts[i].state = True
|
|
|
|
if cancel_all:
|
|
|
|
ts[i].cancel()
|
|
|
|
elif isinstance(ts[i].data, StopIteration):
|
|
|
|
# Sub-task ran to completion, get its return value.
|
|
|
|
ts[i] = ts[i].data.value
|
|
|
|
else:
|
|
|
|
# Sub-task had an exception with return_exceptions==True, so get its exception.
|
|
|
|
ts[i] = ts[i].data
|
|
|
|
|
|
|
|
# Either this gather was cancelled, or one of the sub-tasks raised an exception with
|
|
|
|
# return_exceptions==False, so reraise the exception here.
|
|
|
|
if state is not 0:
|
|
|
|
raise state
|
|
|
|
|
|
|
|
# Return the list of return values of each sub-task.
|
2019-11-13 05:07:58 -05:00
|
|
|
return ts
|