@@ -469,3 +469,47 @@ pyodide.runPython(`
469
469
print (js_lambda, js_lambda_, js_lambda__) # 7, 7, 8
470
470
`);
471
471
```
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