Fix long running processes

This commit is contained in:
Tom 2024-07-29 14:37:06 +01:00
parent a234320e0a
commit aa2e97cea2
5 changed files with 10 additions and 20 deletions

View File

@ -79,13 +79,6 @@ mp.runPython("import fonts; print(fonts.gunship30)")
Note that I've got access to my compiled in code here.
<section class = "micropython-simulator">
<div id="editor"></div>
<button id=run title="Run code (Ctrl-Enter)" aria-title="Run code (Ctrl-Enter)">Run</button>
<canvas height="240" width="240" class = screen></canvas>
<pre id="micropython-stdout"></pre>
</section>
<script src="{{page.assets}}/cm6.bundle.min.js"></script>
<script src="{{page.assets}}/simulator.js" type = "module"></script>

View File

@ -1,6 +1,6 @@
import asyncio
import gc
import struct
import time
from array import array
import console
@ -102,7 +102,10 @@ while True:
)
display.draw(buf)
# The 'await' is necessary here to yield back to the JS event loop
# I tried to figure out how to hide this inside the JS implementation of sleep but
# couldn't make it work.
await time.sleep(0.2)
# Note: Because of the way the webassembly port works, this code is actually running like an asyncio thread
# This call to asyncio.sleep yeilds back to the JS event loop and gives the browser a chance to update the display.
# This is not needed on a real device.
# There is way to make it so that a bare time.sleep() will work but it requires emcripten's ASYNCIFY feature
# Which apparently kills performance. See https://github.com/tomhodson/micropython/commit/2fa6373d226b65f977486ecda32b8786cd1dceed
await asyncio.sleep(0.2)

File diff suppressed because one or more lines are too long

View File

@ -6,9 +6,6 @@ class USBCPowerSupplySimulator extends HTMLElement {
});
mp.registerJsModule("console", { log: (s) => console.log(s) });
mp.registerJsModule("time", {
sleep: async (s) => await new Promise((r) => setTimeout(r, s * 1000)),
});
mp.registerJsModule("display", {
draw: (buf) => {
let bytes = [...buf].flatMap((x) => [x, x, x, 255]);
@ -45,10 +42,7 @@ class USBCPowerSupplySimulator extends HTMLElement {
mp_console.scrollTo(0, mp_console.scrollHeight);
};
const [mp, view] = await Promise.all([
this.init_mp(ctx, stdoutWriter),
this.init_editor(),
]);
const view = await this.init_editor();
const runPython = async () => {
mp_console.innerText = "";