|
| 1 | +""" |
| 2 | +Asynchronous I/O scheduler for writing concurrent code. |
| 3 | +
|
| 4 | +MicroPython module: https://docs.micropython.org/en/v1.22.0/library/asyncio.html |
| 5 | +
|
| 6 | +CPython module: |
| 7 | +`asyncio `<https://docs.python.org/3.8/library/asyncio.html> |
| 8 | +
|
| 9 | +Example:: |
| 10 | +
|
| 11 | + import asyncio |
| 12 | +
|
| 13 | + async def blink(led, period_ms): |
| 14 | + while True: |
| 15 | + led.on() |
| 16 | + await asyncio.sleep_ms(5) |
| 17 | + led.off() |
| 18 | + await asyncio.sleep_ms(period_ms) |
| 19 | +
|
| 20 | + async def main(led1, led2): |
| 21 | + asyncio.create_task(blink(led1, 700)) |
| 22 | + asyncio.create_task(blink(led2, 400)) |
| 23 | + await asyncio.sleep_ms(10_000) |
| 24 | +
|
| 25 | + # Running on a pyboard |
| 26 | + from pyb import LED |
| 27 | + asyncio.run(main(LED(1), LED(2))) |
| 28 | +
|
| 29 | + # Running on a generic board |
| 30 | + from machine import Pin |
| 31 | + asyncio.run(main(Pin(1), Pin(2))) |
| 32 | +
|
| 33 | +Core functions |
| 34 | +-------------- |
| 35 | +""" |
| 36 | +from _typeshed import Incomplete, Incomplete as Incomplete |
| 37 | +from typing import Any, Coroutine, List, Tuple |
| 38 | + |
| 39 | +def ticks_diff(*args, **kwargs) -> Incomplete: ... |
| 40 | +def get_event_loop() -> Incomplete: |
| 41 | + """ |
| 42 | + Return the event loop used to schedule and run tasks. See `Loop`. |
| 43 | + """ |
| 44 | + ... |
| 45 | + |
| 46 | +def current_task() -> Task: |
| 47 | + """ |
| 48 | + Return the `Task` object associated with the currently running task. |
| 49 | + """ |
| 50 | + ... |
| 51 | + |
| 52 | +def create_task(coro) -> Task: |
| 53 | + """ |
| 54 | + Create a new task from the given coroutine and schedule it to run. |
| 55 | +
|
| 56 | + Returns the corresponding `Task` object. |
| 57 | + """ |
| 58 | + ... |
| 59 | + |
| 60 | +def new_event_loop() -> Incomplete: |
| 61 | + """ |
| 62 | + Reset the event loop and return it. |
| 63 | +
|
| 64 | + Note: since MicroPython only has a single event loop this function just |
| 65 | + resets the loop's state, it does not create a new one. |
| 66 | + """ |
| 67 | + ... |
| 68 | + |
| 69 | +def ticks(*args, **kwargs) -> Incomplete: ... |
| 70 | +def run_until_complete(*args, **kwargs) -> Incomplete: ... |
| 71 | +def run(coro) -> Incomplete: |
| 72 | + """ |
| 73 | + Create a new task from the given coroutine and run it until it completes. |
| 74 | +
|
| 75 | + Returns the value returned by *coro*. |
| 76 | + """ |
| 77 | + ... |
| 78 | + |
| 79 | +def wait_for_ms(awaitable, timeout) -> Coroutine[Incomplete, Any, Any]: |
| 80 | + """ |
| 81 | + Similar to `wait_for` but *timeout* is an integer in milliseconds. |
| 82 | +
|
| 83 | + This is a coroutine, and a MicroPython extension. |
| 84 | + """ |
| 85 | + ... |
| 86 | + |
| 87 | +def sleep_ms(t) -> Coroutine[Incomplete, Any, Any]: |
| 88 | + """ |
| 89 | + Sleep for *t* milliseconds. |
| 90 | +
|
| 91 | + This is a coroutine, and a MicroPython extension. |
| 92 | + """ |
| 93 | + ... |
| 94 | + |
| 95 | +def ticks_add(*args, **kwargs) -> Incomplete: ... |
| 96 | +def sleep(t) -> Coroutine[Incomplete, Any, Any]: |
| 97 | + """ |
| 98 | + Sleep for *t* seconds (can be a float). |
| 99 | +
|
| 100 | + This is a coroutine. |
| 101 | + """ |
| 102 | + ... |
| 103 | + |
| 104 | +class TaskQueue: |
| 105 | + def push(self, *args, **kwargs) -> Incomplete: ... |
| 106 | + def peek(self, *args, **kwargs) -> Incomplete: ... |
| 107 | + def remove(self, *args, **kwargs) -> Incomplete: ... |
| 108 | + def pop(self, *args, **kwargs) -> Incomplete: ... |
| 109 | + def __init__(self, *argv, **kwargs) -> None: ... |
| 110 | + |
| 111 | +open_connection: Incomplete |
| 112 | + |
| 113 | +class Event: |
| 114 | + """ |
| 115 | + Create a new event which can be used to synchronise tasks. Events start |
| 116 | + in the cleared state. |
| 117 | + """ |
| 118 | + |
| 119 | + def set(self) -> None: |
| 120 | + """ |
| 121 | + Set the event. Any tasks waiting on the event will be scheduled to run. |
| 122 | +
|
| 123 | + Note: This must be called from within a task. It is not safe to call this |
| 124 | + from an IRQ, scheduler callback, or other thread. See `ThreadSafeFlag`. |
| 125 | + """ |
| 126 | + ... |
| 127 | + def is_set(self) -> bool: |
| 128 | + """ |
| 129 | + Returns ``True`` if the event is set, ``False`` otherwise. |
| 130 | + """ |
| 131 | + ... |
| 132 | + def clear(self) -> None: |
| 133 | + """ |
| 134 | + Clear the event. |
| 135 | + """ |
| 136 | + ... |
| 137 | + wait: Incomplete |
| 138 | + def __init__(self, *argv, **kwargs) -> None: ... |
| 139 | + |
| 140 | +class Lock: |
| 141 | + """ |
| 142 | + Create a new lock which can be used to coordinate tasks. Locks start in |
| 143 | + the unlocked state. |
| 144 | +
|
| 145 | + In addition to the methods below, locks can be used in an ``async with`` statement. |
| 146 | + """ |
| 147 | + |
| 148 | + def locked(self) -> bool: |
| 149 | + """ |
| 150 | + Returns ``True`` if the lock is locked, otherwise ``False``. |
| 151 | + """ |
| 152 | + ... |
| 153 | + def release(self) -> Incomplete: |
| 154 | + """ |
| 155 | + Release the lock. If any tasks are waiting on the lock then the next one in the |
| 156 | + queue is scheduled to run and the lock remains locked. Otherwise, no tasks are |
| 157 | + waiting an the lock becomes unlocked. |
| 158 | + """ |
| 159 | + ... |
| 160 | + acquire: Incomplete |
| 161 | + def __init__(self, *argv, **kwargs) -> None: ... |
| 162 | + |
| 163 | +class Task: |
| 164 | + """ |
| 165 | + This object wraps a coroutine into a running task. Tasks can be waited on |
| 166 | + using ``await task``, which will wait for the task to complete and return |
| 167 | + the return value of the task. |
| 168 | +
|
| 169 | + Tasks should not be created directly, rather use `create_task` to create them. |
| 170 | + """ |
| 171 | + |
| 172 | + def __init__(self, *argv, **kwargs) -> None: ... |
| 173 | + |
| 174 | +wait_for: Incomplete |
| 175 | + |
| 176 | +class CancelledError(Exception): ... |
0 commit comments