When I come to the PyO3 frontpage, having never seen PyO3 before, my first questions are "how is this different from rust-cpython?" and "Why would I want to use this instead?"
So far, all I've been able to determine is:
ToPyObject impls being this long in rust-cpython.It'd be a good idea to add a blurb to the README explaining:
at this point, base concepts, compared to rust-cpython, are totally different.
rust-cpython:
impl PyList {
fn new(py: Python) -> PyList {...}
fn get_item(&self, py: Python, index: isize) -> PyObject {...}
}
pyo3:
impl PyList {
fn new(py: Python) -> &PyList {...}
fn get_item(&self, index: isize) -> &PyObject {...}
}
Because pyo3 allows only references to python object, all refs uses Gil lifetime. So Python object
is not required, and it is safe to have fn py<'p>(&'p self) -> Python<'p> {} function.
rust-cpython custom class
rust-cpython uses whole new language based on macrospy_class! macros, for example async/await support.pyo3 custom class
proc_macro and specialization for class implementation (nightly is required)pyo3 does not modify rust struct. it is possible to define needed traits and make rust type compatible with python class without using #[py::class] macro.specialization and traits and associated types. separate trait is defined for each typemotivation
downsides:
In that case, definitely something I'll be switching over to once it's possible to resolve #5.
I used rust-cpython, and had occasional segfaults. never got any response from author.
I use pyo3 in production without this segfault.
In that case, once I've finished migrating as much of my project to the Rust side as possible, if I encounter segfaults and can't resolve them, maybe I'll drop to something lower-level like Snaek.
...or just port everything back to pure Python. Avoiding dependency on nightly Rust is non-negotiable.
I hope proc_macro will be stabilized by the end of year.
Just landed pretty big change.
rust-cpython requires Python parameter for PyErr, so error handling ergonomics is pretty bad.
It is not possible to use ? with rust errors.
pyo3 on other hand does not require Python for PyErr creation, it is required only if you want to set exception to python interpreter with PyErr::restore() method. so it is possible to use std::convert::From<Err> for PyErr trait and ? is supported automatically.
for example:
use pyo3::*;
fn parse_int(s: String) -> PyResult<usize> {
Ok(s.parse::<usize>()?)
}
The code snippet above will raise ValueError in Python if String::parse() return an error.
closing, there is link in readme
Does rust_cpytho support all Python packages like socket?
@MajidHeydari I don't see why it wouldn't.
There are two major reasons something might not support a Python package:
The former isn't a problem because rust-cpython embeds or produces compiled extensions for the actual CPython.
As for the latter, rust-cpython won't cause incompatibilities, but it's still up to you to avoid using Python extensions and Rust crates which depend on incompatible C libraries at the same time. (eg. Trying to use both the Python and Rust bindings for the same C library in the same process, rather than using just one and using rust-cpython to share it across the language barrier.)
can rust_cpytho build web framework ?
To what end?
Combining Python with another language isn't going to magically make it faster because the things holding back Python's speed are inherent and changing them would break compatibility with existing Python packages.
You can use rust-cpython to write compiled modules for a Python web framework to load, but things like Python's Global Interpreter Lock and highly dynamic nature aren't going to perform well if you try to write a web framework by embedding a Python runtime into Rust.
(And, if you've got CPU-bound tasks in your web app, you're supposed to move them out into something like Celery anyway so they don't block the part of the app handling the web requests.)
Does webassembly not make the server faster?
WebAssembly isn't magic sauce. You need to use it to replace the bits that are slow... and if all the low-hanging fruit has been picked and the remaining slowness is spread throughout the language because of things like extremely dynamic dispatch, then you can't really "replace" it without rewriting everything in another language.
Most helpful comment
rust-cpythoncustom classrust-cpythonuses whole new language based on macrospy_class!macros, for exampleasync/awaitsupport.pyo3custom classproc_macroandspecializationfor class implementation (nightly is required)pyo3does not modify rust struct. it is possible to define needed traits and make rust type compatible with python class without using#[py::class]macro.specializationand traits and associated types. separate trait is defined for each typeof customization (i.e. PyMappingProtocol, PyNumberProtocol). macro is used for simplicity, but it is possible to make rust type compatible with specific customization without using proc macros.