-
-
Notifications
You must be signed in to change notification settings - Fork 599
Expand file tree
/
Copy pathsubinterpreters.py
More file actions
52 lines (38 loc) Β· 1.66 KB
/
subinterpreters.py
File metadata and controls
52 lines (38 loc) Β· 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
"""
Multi-interpreter support via concurrent.interpreters in Python 3.14 (PEP 734).
Python subinterpreters allow running Python code in truly isolated
environments within a single process. In Python 3.14, each interpreter
has its own Global Interpreter Lock (GIL), enabling true multi-core
parallelism for Python code.
"""
import concurrent.futures
import concurrent.interpreters as interpreters
def run_worker(data):
"""Function to be run in a subinterpreter."""
# This code runs in a different interpreter environment
return f"Processed {data} in a subinterpreter"
def main():
# 1. Create a subinterpreter
# The 'interpreters' module provides a high-level API
interp = interpreters.create()
assert isinstance(interp, interpreters.Interpreter)
assert interp.is_running() is False
# 2. Run code in the subinterpreter
# We can run simple strings of code
interp.exec("1 + 1")
# 3. Using InterpreterPoolExecutor for easier management
# This is similar to ThreadPoolExecutor or ProcessPoolExecutor
with concurrent.futures.InterpreterPoolExecutor(max_workers=2) as executor:
# Submit tasks to the pool
future1 = executor.submit(run_worker, "Task A")
future2 = executor.submit(run_worker, "Task B")
# Get results (this handles passing data back and forth)
result1 = future1.result()
result2 = future2.result()
assert result1 == "Processed Task A in a subinterpreter"
assert result2 == "Processed Task B in a subinterpreter"
# 4. Cleaning up
interp.close()
assert interp.id not in [i.id for i in interpreters.list_all()]
if __name__ == "__main__":
main()