async_py
A Rust library for calling Python code asynchronously using pyo3 and tokio.
This library provides a PyRunner that runs a Python interpreter in a dedicated
background thread. Global variables are preserved within the runner's scope.
You can send Python code to this executor from any async Rust task,
and it will be executed without blocking your application.
Due to the Python Global Interpreter Lock (GIL), you cannot run Python code simultaneously. If one Python task is performing a computation or a sleep, no other task can access the interpreter.
Using multiple PyRunner instances does not enable simultaneous Python code execution due to the GIL.
However, it does allow you to isolate global variables between different runners.
Usage
Add async_py to your Cargo.toml:
[]
= "0.2.0"
= { = "1", = ["full"] }
Example
use PyRunner;
async
Using a venv
It is generally recommended to use a venv to install pip packages. While you cannot switch the interpreter version with this crate, you can use an existing venv to load installed packages.
let runner = new;
runner.set_venv.await?;
rustpython
PyO3 has usually the best compatibility for python packages.
But if you want to use python on android, wasm, or if you don't want to use any
external library, you can use rustpython.
Keep in mind that some essential packages like os are missing on rustpython.
Cargo.toml
[]
= { = ["rustpython"] }