This repository provides a procedural macro for generating Python clients from Rust tarpc
servers automatically. Using PyO3 and pyo3-asyncio, it allows seamless Python integration with Rust RPC endpoints while supporting all serde
-serializable data types.
In the future, this project aims to be able to generate pyi
files in order to be able to provide type information and signatures of the generated packages for the python clients
- Automatic client generation for Python from Rust tarpc services
- Supports async Python clients using
pyo3-asyncio
- Handles all
serde
-serializable data types, not just basic primitives - Easy integration with existing Rust + tarpc projects
- Minimal boilerplate for Python-Rust RPC communication
- Python 3.8+
- Rust (with cargo)
- maturin (for building Python extensions)
# Navigate to your Rust project folder
maturin develop -r
- Define your Rust tarpc service:
#[tarpc_python_client]
#[tarpc::service]
pub trait RpcAPI {
async fn hellow(name: String) -> i32;
}
- Call the Python client:
from rpc_model import PyRpcAPIClient
import asyncio
async def main():
stub = await PyRpcAPIClient.connect("127.0.0.1:5000")
response = await stub.hello("Diego")
print(response)
asyncio.run(main())
The example/
folder contains a minimal working example demonstrating:
- RPC server implemented in Rust (
tarpc-server/
) - Rust models compiled as Python extensions (
rpc-model/
) - Python client connecting to the server (
client.py
)
Run the example by following the steps in example/README.md
.
- Procedural Macro: Generates Python bindings for the Rust tarpc service
- PyO3 Integration: Exposes Rust functions and structs to Python
- Async Python Clients: Uses
pyo3-asyncio
to handle async calls natively - Serde Support: Any Rust type that implements
Serialize
/Deserialize
works seamlessly
my-project/
│
├── macros/ # Procedural macro code
├── rpc-model/ # Rust models compiled as Python modules
├── tarpc-server/ # Rust tarpc server implementations
├── example/ # Example demonstrating Python client usage
└── client/ # Optional standalone Python clients
Contributions, bug reports, and feature requests are welcome! Please open an issue or submit a pull request.
This project is licensed under the MIT License.