New submission from Hood Chatham <roberthoodchat...@gmail.com>:

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).

----------
components: Interpreter Core
messages: 416403
nosy: hoodmane
priority: normal
severity: normal
status: open
title: Interrupt handling for wasm32-emscripten builds without pthreads
type: enhancement
versions: Python 3.10

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue47176>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to