|
| 1 | +# MicroPython asyncio module, for use with webassembly port |
| 2 | +# MIT license; Copyright (c) 2019-2024 Damien P. George |
| 3 | + |
| 4 | +from time import ticks_ms as ticks, ticks_diff, ticks_add |
| 5 | +import sys, js, jsffi |
| 6 | + |
| 7 | +# Import TaskQueue and Task from built-in C code. |
| 8 | +from _asyncio import TaskQueue, Task |
| 9 | + |
| 10 | + |
| 11 | +################################################################################ |
| 12 | +# Exceptions |
| 13 | + |
| 14 | + |
| 15 | +class CancelledError(BaseException): |
| 16 | + pass |
| 17 | + |
| 18 | + |
| 19 | +class TimeoutError(Exception): |
| 20 | + pass |
| 21 | + |
| 22 | + |
| 23 | +# Used when calling Loop.call_exception_handler. |
| 24 | +_exc_context = {"message": "Task exception wasn't retrieved", "exception": None, "future": None} |
| 25 | + |
| 26 | + |
| 27 | +################################################################################ |
| 28 | +# Sleep functions |
| 29 | + |
| 30 | + |
| 31 | +# "Yield" once, then raise StopIteration |
| 32 | +class SingletonGenerator: |
| 33 | + def __init__(self): |
| 34 | + self.state = None |
| 35 | + self.exc = StopIteration() |
| 36 | + |
| 37 | + def __iter__(self): |
| 38 | + return self |
| 39 | + |
| 40 | + def __next__(self): |
| 41 | + if self.state is not None: |
| 42 | + _task_queue.push(cur_task, self.state) |
| 43 | + self.state = None |
| 44 | + return None |
| 45 | + else: |
| 46 | + self.exc.__traceback__ = None |
| 47 | + raise self.exc |
| 48 | + |
| 49 | + |
| 50 | +# Pause task execution for the given time (integer in milliseconds, uPy extension) |
| 51 | +# Use a SingletonGenerator to do it without allocating on the heap |
| 52 | +def sleep_ms(t, sgen=SingletonGenerator()): |
| 53 | + if cur_task is None: |
| 54 | + # Support top-level asyncio.sleep, via a JavaScript Promise. |
| 55 | + return jsffi.async_timeout_ms(t) |
| 56 | + assert sgen.state is None |
| 57 | + sgen.state = ticks_add(ticks(), max(0, t)) |
| 58 | + return sgen |
| 59 | + |
| 60 | + |
| 61 | +# Pause task execution for the given time (in seconds) |
| 62 | +def sleep(t): |
| 63 | + return sleep_ms(int(t * 1000)) |
| 64 | + |
| 65 | + |
| 66 | +################################################################################ |
| 67 | +# Main run loop |
| 68 | + |
| 69 | +asyncio_timer = None |
| 70 | + |
| 71 | + |
| 72 | +class ThenableEvent: |
| 73 | + def __init__(self, thenable): |
| 74 | + self.result = None # Result of the thenable |
| 75 | + self.waiting = None # Task waiting on completion of this thenable |
| 76 | + thenable.then(self.set) |
| 77 | + |
| 78 | + def set(self, value): |
| 79 | + # Thenable/Promise is fulfilled, set result and schedule any waiting task. |
| 80 | + self.result = value |
| 81 | + if self.waiting: |
| 82 | + _task_queue.push(self.waiting) |
| 83 | + self.waiting = None |
| 84 | + _schedule_run_iter(0) |
| 85 | + |
| 86 | + def remove(self, task): |
| 87 | + self.waiting = None |
| 88 | + |
| 89 | + # async |
| 90 | + def wait(self): |
| 91 | + # Set the calling task as the task waiting on this thenable. |
| 92 | + self.waiting = cur_task |
| 93 | + # Set calling task's data to this object so it can be removed if needed. |
| 94 | + cur_task.data = self |
| 95 | + # Wait for the thenable to fulfill. |
| 96 | + yield |
| 97 | + # Return the result of the thenable. |
| 98 | + return self.result |
| 99 | + |
| 100 | + |
| 101 | +# Ensure the awaitable is a task |
| 102 | +def _promote_to_task(aw): |
| 103 | + return aw if isinstance(aw, Task) else create_task(aw) |
| 104 | + |
| 105 | + |
| 106 | +def _schedule_run_iter(dt): |
| 107 | + global asyncio_timer |
| 108 | + if asyncio_timer is not None: |
| 109 | + js.clearTimeout(asyncio_timer) |
| 110 | + asyncio_timer = js.setTimeout(_run_iter, dt) |
| 111 | + |
| 112 | + |
| 113 | +def _run_iter(): |
| 114 | + global cur_task |
| 115 | + excs_all = (CancelledError, Exception) # To prevent heap allocation in loop |
| 116 | + excs_stop = (CancelledError, StopIteration) # To prevent heap allocation in loop |
| 117 | + while True: |
| 118 | + # Wait until the head of _task_queue is ready to run |
| 119 | + t = _task_queue.peek() |
| 120 | + if t: |
| 121 | + # A task waiting on _task_queue; "ph_key" is time to schedule task at |
| 122 | + dt = max(0, ticks_diff(t.ph_key, ticks())) |
| 123 | + else: |
| 124 | + # No tasks can be woken so finished running |
| 125 | + cur_task = None |
| 126 | + return |
| 127 | + |
| 128 | + if dt > 0: |
| 129 | + # schedule to call again later |
| 130 | + cur_task = None |
| 131 | + _schedule_run_iter(dt) |
| 132 | + return |
| 133 | + |
| 134 | + # Get next task to run and continue it |
| 135 | + t = _task_queue.pop() |
| 136 | + cur_task = t |
| 137 | + try: |
| 138 | + # Continue running the coroutine, it's responsible for rescheduling itself |
| 139 | + exc = t.data |
| 140 | + if not exc: |
| 141 | + t.coro.send(None) |
| 142 | + else: |
| 143 | + # If the task is finished and on the run queue and gets here, then it |
| 144 | + # had an exception and was not await'ed on. Throwing into it now will |
| 145 | + # raise StopIteration and the code below will catch this and run the |
| 146 | + # call_exception_handler function. |
| 147 | + t.data = None |
| 148 | + t.coro.throw(exc) |
| 149 | + except excs_all as er: |
| 150 | + # Check the task is not on any event queue |
| 151 | + assert t.data is None |
| 152 | + # This task is done. |
| 153 | + if t.state: |
| 154 | + # Task was running but is now finished. |
| 155 | + waiting = False |
| 156 | + if t.state is True: |
| 157 | + # "None" indicates that the task is complete and not await'ed on (yet). |
| 158 | + t.state = None |
| 159 | + elif callable(t.state): |
| 160 | + # The task has a callback registered to be called on completion. |
| 161 | + t.state(t, er) |
| 162 | + t.state = False |
| 163 | + waiting = True |
| 164 | + else: |
| 165 | + # Schedule any other tasks waiting on the completion of this task. |
| 166 | + while t.state.peek(): |
| 167 | + _task_queue.push(t.state.pop()) |
| 168 | + waiting = True |
| 169 | + # "False" indicates that the task is complete and has been await'ed on. |
| 170 | + t.state = False |
| 171 | + if not waiting and not isinstance(er, excs_stop): |
| 172 | + # An exception ended this detached task, so queue it for later |
| 173 | + # execution to handle the uncaught exception if no other task retrieves |
| 174 | + # the exception in the meantime (this is handled by Task.throw). |
| 175 | + _task_queue.push(t) |
| 176 | + # Save return value of coro to pass up to caller. |
| 177 | + t.data = er |
| 178 | + elif t.state is None: |
| 179 | + # Task is already finished and nothing await'ed on the task, |
| 180 | + # so call the exception handler. |
| 181 | + |
| 182 | + # Save exception raised by the coro for later use. |
| 183 | + t.data = exc |
| 184 | + |
| 185 | + # Create exception context and call the exception handler. |
| 186 | + _exc_context["exception"] = exc |
| 187 | + _exc_context["future"] = t |
| 188 | + Loop.call_exception_handler(_exc_context) |
| 189 | + |
| 190 | + |
| 191 | +# Create and schedule a new task from a coroutine. |
| 192 | +def create_task(coro): |
| 193 | + if not hasattr(coro, "send"): |
| 194 | + raise TypeError("coroutine expected") |
| 195 | + t = Task(coro, globals()) |
| 196 | + _task_queue.push(t) |
| 197 | + _schedule_run_iter(0) |
| 198 | + return t |
| 199 | + |
| 200 | + |
| 201 | +################################################################################ |
| 202 | +# Event loop wrapper |
| 203 | + |
| 204 | + |
| 205 | +cur_task = None |
| 206 | + |
| 207 | + |
| 208 | +class Loop: |
| 209 | + _exc_handler = None |
| 210 | + |
| 211 | + def create_task(coro): |
| 212 | + return create_task(coro) |
| 213 | + |
| 214 | + def close(): |
| 215 | + pass |
| 216 | + |
| 217 | + def set_exception_handler(handler): |
| 218 | + Loop._exc_handler = handler |
| 219 | + |
| 220 | + def get_exception_handler(): |
| 221 | + return Loop._exc_handler |
| 222 | + |
| 223 | + def default_exception_handler(loop, context): |
| 224 | + print(context["message"], file=sys.stderr) |
| 225 | + print("future:", context["future"], "coro=", context["future"].coro, file=sys.stderr) |
| 226 | + sys.print_exception(context["exception"], sys.stderr) |
| 227 | + |
| 228 | + def call_exception_handler(context): |
| 229 | + (Loop._exc_handler or Loop.default_exception_handler)(Loop, context) |
| 230 | + |
| 231 | + |
| 232 | +def get_event_loop(): |
| 233 | + return Loop |
| 234 | + |
| 235 | + |
| 236 | +def current_task(): |
| 237 | + if cur_task is None: |
| 238 | + raise RuntimeError("no running event loop") |
| 239 | + return cur_task |
| 240 | + |
| 241 | + |
| 242 | +def new_event_loop(): |
| 243 | + global _task_queue |
| 244 | + _task_queue = TaskQueue() # TaskQueue of Task instances. |
| 245 | + return Loop |
| 246 | + |
| 247 | + |
| 248 | +# Initialise default event loop. |
| 249 | +new_event_loop() |
0 commit comments