Hot*_*hen 5 python rust python-3.8 pyo3
我想在 Python 中使用 Rust 异步方法。我正在尝试使用PyO3或rust-cpython。
例如,对于同步 Rust 函数,我可以使用,
#[pyfunction]
fn myfunc(a: String) -> PyResult<String> {
let mut contents = String::new();
contents = a.to_string() + " appended";
Ok((contents))
}
#[pymodule]
fn MyModule(py: Python, m: &PyModule) -> PyResult<()> {
m.add_wrapped(wrap_pyfunction!(urlshot))?;
Ok(())
}
Run Code Online (Sandbox Code Playgroud)
对于异步方法,我该怎么做?例如,我想在 Python 中调用以下方法,
async fn hello_world() {
println!("hello, world!");
}
Run Code Online (Sandbox Code Playgroud)
由于没有简单的方法来解决这个问题(至少,我还没有找到),我将我的异步方法转换为同步方法。并在 Python 端称它为,
async fn my_method(s: &str) -> Result<String, Error> {
// do something
}
#[pyfunction]
fn my_sync_method(s: String) -> PyResult<String> {
let mut rt = tokio::runtime::Runtime::new().unwrap();
let mut contents = String::new();
rt.block_on(async {
result = format!("{}", my_sync_method(&s).await.unwrap()).to_string();
});
Ok((result))
}
#[pymodule]
fn MyModule(py: Python, m: &PyModule) -> PyResult<()> {
m.add_wrapped(wrap_pyfunction!(my_sync_method))?;
Ok(())
}
Run Code Online (Sandbox Code Playgroud)
在Cargo.toml文件中,我添加了以下依赖项,
[dependencies.pyo3]
git = "https://github.com/PyO3/pyo3"
features = ["extension-module"]
Run Code Online (Sandbox Code Playgroud)
运行后cargo build --release,target/release/libMyModule.so生成二进制文件。将其重命名为MyModule.so,现在可以从 Python 导入。
[dependencies.pyo3]
git = "https://github.com/PyO3/pyo3"
features = ["extension-module"]
Run Code Online (Sandbox Code Playgroud)
使用setuptools-rust,我可以将它捆绑为一个普通的 Python 包。
以上所有代码和命令都在新发布的 Linux Mint 20 上进行了测试。在 MacOS 上,二进制文件将为libMyModule.dylib.