This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author hoodmane
Recipients hoodmane
Date 2022-03-30.21:55:22
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <>
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).
Date User Action Args
2022-03-30 21:55:22hoodmanesetrecipients: + hoodmane
2022-03-30 21:55:22hoodmanesetmessageid: <>
2022-03-30 21:55:22hoodmanelinkissue47176 messages
2022-03-30 21:55:22hoodmanecreate