Skip to content

Commit 0b94ff3

Browse files
committed
docs: add Python event-loop example/trick
1 parent 46d59e5 commit 0b94ff3

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,26 @@ globalThis.python.exit = pm.eval("""'use strict';
273273
""")(sys.exit);
274274
```
275275

276+
### Run Python event-loop
277+
278+
You need an event-loop running to use `setTimeout` and `Promise`<=>`awaitable` coercion.
279+
280+
```python
281+
import asyncio
282+
283+
async def async_fn():
284+
await pm.eval("""
285+
new Promise((resolve) => setTimeout((...args) => {
286+
console.log(args);
287+
resolve();
288+
}, 1000, 42, "abc")
289+
)
290+
""")
291+
await pm.eval("async (x) => await x")(asyncio.sleep(0.5))
292+
293+
asyncio.run(async_fn())
294+
```
295+
276296
# pmjs
277297
A basic JavaScript shell, `pmjs`, ships with PythonMonkey. This shell can act as a REPL or run
278298
JavaScript programs; it is conceptually similar to the `node` shell which ships with Node.js.

0 commit comments

Comments
 (0)