mirror of
https://github.com/micropython/micropython.git
synced 2025-12-16 01:40:14 +01:00
This change allows doing a top-level await on an asyncio primitive like Task and Event. This feature enables a better interaction and synchronisation between JavaScript and Python, because `api.runPythonAsync` can now be used (called from JavaScript) to await on the completion of asyncio primitives. Signed-off-by: Damien George <damien@micropython.org>
26 lines
516 B
JavaScript
26 lines
516 B
JavaScript
// Test top-level await on asyncio primitives: Task, Event.
|
|
|
|
const mp = await (await import(process.argv[2])).loadMicroPython();
|
|
|
|
await mp.runPythonAsync(`
|
|
import asyncio
|
|
|
|
async def task(event):
|
|
print("task set event")
|
|
event.set()
|
|
print("task sleep")
|
|
await asyncio.sleep(0.1)
|
|
print("task end")
|
|
|
|
event = asyncio.Event()
|
|
t = asyncio.create_task(task(event))
|
|
|
|
print("top-level wait event")
|
|
await event.wait()
|
|
print("top-level wait task")
|
|
await t
|
|
print("top-level end")
|
|
`);
|
|
|
|
console.log("finished");
|