当我们编写香草锈病并且必须将异步函数作为参数传递给另一个函数时,我们执行以下操作:
pub f<F,'a>(
test: &dyn Fn(&'a mut String, String, String, TcpStream) -> F,
) where
F: Future<Output = ()> + 'a,但是,当我在一个#![pyfunction]上做同样的事情,希望得到一个异步python函数时,我会得到一个错误。
e.g async def fn():
....在阅读PyO3的文档时,我发现我可以将PyAny作为param包括在内。
但是,关于执行以下职能:
pub fn start_server(test: PyAny) {
test.call0();
}我得到以下错误。
[rustc E0277] [E] the trait bound `pyo3::PyAny: pyo3::FromPyObject<'_>` is not satisfied
expected an implementor of trait `pyo3::FromPyObject<'_>`
note: required because of the requirements on the impl of `pyo3::FromPyObject<'_>` for `pyo3::PyAny`如何在我的代码中实现这一点。如果这是不可能的话,我会理解,如果是这样的话,我会请你推荐我另一种选择。
更新:
我已经找到了另一种方法,在这里我创建了一个空的结构,并以以下方式调用该方法。但是如果我能不创建一个空结构就能通过,我会非常感激的。
#[pymethods]
impl Server {
#[new]
fn new() -> Self {
Self {}
}
fn start(mut self_: PyRefMut<Self>, test: &PyAny) {
test.call0();
}
}但是,如果将异步函数作为param传递,则会出现
RuntimeWarning: coroutine
s.start(h)
RuntimeWarning: Enable tracemalloc to get the object allocation traceback发布于 2021-05-25 11:51:54
您的函数需要引用,即&PyAny。PyAny作为一个拥有的值并不实现FromPyObject,这就是您得到错误的原因。
// lib.rs
use pyo3::prelude::*;
use pyo3::wrap_pyfunction;
#[pyfunction]
fn foo(x: &PyAny) -> PyResult<&PyAny> {
x.call0()
}
#[pymodule]
fn async_pyo3(py: Python, m: &PyModule) -> PyResult<()> {
m.add_function(wrap_pyfunction!(foo, m)?).unwrap();
Ok(())
}import async_pyo3
async def bar():
return "foo"
awaitable = async_pyo3.foo(bar) # <coroutine object bar at 0x7f8f6aa01340>
print(await awaitable) # "foo"因此,将其移动到Server上的方法的修正很可能不是修复,而是巧合,因为您将test更改为&PyAny。
关于集成Python和Rust异步/等待,有PyO3文档中的整个部分
https://stackoverflow.com/questions/67686409
复制相似问题