Skip to content

Commit 77d6b41

Browse files
authored
Mention missing multi-threading/processing in faq and new-packages docs (pyodide#5287)
1 parent 51bc9ff commit 77d6b41

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

docs/development/new-packages.md

+16
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,22 @@ Most pure Python packages can be installed directly from PyPI with
1717
{func}`micropip.install` if they have a pure Python wheel. Check if this is the
1818
case by trying `micropip.install("package-name")`.
1919

20+
Because Pyodide [does not support threading or multiprocessing](https://pyodide.org/en/stable/usage/wasm-constraints.html),
21+
packages that use threading or multiprocessing will not work without a patch to disable it. For example,
22+
the following snippet will determine if the platform supports creating new threads.
23+
24+
```py
25+
def _can_start_thread() -> bool:
26+
if sys.platform == "emscripten":
27+
return sys._emscripten_info.pthreads
28+
return platform.machine() not in ("wasm32", "wasm64")
29+
30+
can_start_thread = _can_start_thread()
31+
32+
if not can_start_thread:
33+
n_threads = 1
34+
```
35+
2036
If there is no wheel on PyPI, but you believe there is nothing preventing it (it
2137
is a Python package without C extensions):
2238

docs/usage/faq.md

+44
Original file line numberDiff line numberDiff line change
@@ -469,3 +469,47 @@ pyodide.runPython(`
469469
print(js_lambda, js_lambda_, js_lambda__) # 7, 7, 8
470470
`);
471471
```
472+
473+
## Can I use threading/multiprocessing/subprocess?
474+
475+
No, fork and pthreads do not work in Pyodide (see more [here](https://pyodide.org/en/stable/usage/wasm-constraints.html)).
476+
Attempts to use `threading`, `multiprocessing`, or `subprocess` will raise a `RuntimeError`.
477+
You may be able to work around this by setting the number of threads to 1:
478+
479+
```py
480+
def _can_start_thread() -> bool:
481+
if sys.platform == "emscripten":
482+
return sys._emscripten_info.pthreads
483+
return platform.machine() not in ("wasm32", "wasm64")
484+
485+
can_start_thread = _can_start_thread()
486+
487+
if not can_start_thread:
488+
n_threads = 1
489+
```
490+
491+
You can still import the packages and use the general info API, but you cannot
492+
start any asynchronous work without receiving a `RuntimeError`.
493+
494+
```pycon
495+
>>> import threading
496+
>>> current = threading.current_thread()
497+
>>> current.name
498+
'MainThread'
499+
>>> current.daemon
500+
False
501+
>>> current.is_alive()
502+
True
503+
>>> def target(nums):
504+
... print(sum(nums))
505+
...
506+
>>> t = threading.Thread(target=target, args=([1, 2, 3], ))
507+
>>> t.run()
508+
6
509+
>>> t.start()
510+
Traceback (most recent call last):
511+
File "<console>", line 1, in <module>
512+
File "/lib/python312.zip/threading.py", line 994, in start
513+
_start_new_thread(self._bootstrap, ())
514+
RuntimeError: can't start new thread
515+
```

0 commit comments

Comments
 (0)