You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In Pyodide, we need to patch the interpreter to allow keyboard interrupts. We build Python without pthreads support because Emscripten doesn't currently support using pthreads and dynamic linking in the same build. It is still possible to handle UI at the same time as running Python code by running Python in a web worker. However, the web assembly memory is private to the webworker and cannot be modified from the main thread. The standard way that keyboard interrupts are handled is that the operating system preempts the task to run a signal handler. This can't happen in Emscripten because there is no operating system.
Instead, in Emscripten we create a SharedArrayBuffer, share it with the main thread, and then write the signal into this shared memory from the main thread. We patch the main loop to periodically poll this SharedArrayBuffer and if an interrupt has been requested it calls PyErr_SetInterruptEx to signal the interrupt. I've set the polling rate to once every 50 interpreter operations, which seems to be reasonably responsive and have a negligible performance cost.
One interesting feature of this setup is that it is impossible to create a pointer to the shared memory so it cannot be read directly from C (instead we check it from an EM_ASM block).