https://github.com/chillfish8/python12-subinterpreters
Some simple and hopefully safe-ish bindings to Python 3.12's sub-interpreters.
https://github.com/chillfish8/python12-subinterpreters
Last synced: 2 months ago
JSON representation
Some simple and hopefully safe-ish bindings to Python 3.12's sub-interpreters.
- Host: GitHub
- URL: https://github.com/chillfish8/python12-subinterpreters
- Owner: ChillFish8
- Created: 2023-10-02T21:43:04.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2023-10-02T21:50:16.000Z (over 1 year ago)
- Last Synced: 2025-02-01T14:32:08.934Z (4 months ago)
- Language: Rust
- Size: 7.81 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Python 3.12 Sub-interpreters
These are some safe-ish Rust bindings to the new CFFI API for creating and managing sub-interpreters
within Python.### Prerequisites
You need at least Python 3.12+ (duh)To build and mess with this project you need to have Rust installed which you can get at:
https://www.rust-lang.org/tools/install
### Installation
Once this is installed you can run:```shell
pipenv install
pipenv shell
maturin develop
```Which will build and compile the library which can then be imported as `subinterpreters`.
### Usage
It's a pretty simple API:
```py
from subinterpreters import create_interpreter, SubInterpreternew: SubInterpreter = create_interpreter(
# Some optional config options to mess with:
# allow_exec=False,
# allow_fork=False,
# allow_threads=True,
# allow_daemon_threads=False,
)new.run_code(
"""
import random
print(random.randint(1, 10))
"""
)# You can pass globals and locals
new.run_code(
"""
import random
print(random.randint(1, 10))
""",
globals=globals(),
locals=locals(),
)
```### Notes
Sometimes, if an error occurs, the process will exit with a status code which largely
means "Some memory fucked up" without it being an actual segfault. I don't know why, as far as
I can tell this is correct. But I do know there is still the limitation that sub-interpreters aren't
cleaned up quite correctly by Python if they're still running when the main interpreter shuts down.