如何从 Rust 调用 Python 异步函数?

问题描述

我已阅读此答案(How to call Rust async method from Python?) 并且我想做的与此相反。

我想调用 Python 异步函数并在 tokio 的运行时等待它。可能吗?

我已经尝试了一些,但我在输出中遇到了错误

这是我的python文件的样子:

import rust_package

async def h():
    print("message from coroutine")


print("Hello world")
s = rust_package.Server()
s.start(h)

这就是我的 lib.rs 的样子:

#[pyclass]
struct Server {}

#[pymethods]
impl Server {
    #[new]
    fn new() -> Self {
        Self {}
    }

    fn start(mut self_: PyRefMut<Self>,test: &PyAny) {


        let f = pyo3_asyncio::into_future(test).unwrap();
        let rt = tokio::runtime::Runtime::new().unwrap();
        pyo3_asyncio::tokio::init(rt);
        Python::with_gil(|py| {
            pyo3_asyncio::tokio::run_until_complete(py,async move {
                tokio::time::sleep(Duration::from_secs(1)).await;
                f.await.unwrap();
                Ok(())
            })
            .unwrap();
        });
}

#[pymodule]
pub fn roadrunner(py: Python<'_>,m: &PyModule) -> PyResult<()> {
    m.add_class::<Server>()?;
    pyo3_asyncio::try_init(py);
    Ok(())
}

我收到以下错误

Hello world
Exception in callback <builtins.PyEnsureFuture object a
t 0x10bc1bd20>()
handle: <Handle <builtins.PyEnsureFuture object at 0x10
bc1bd20>()>
Traceback (most recent call last):
  File "/usr/local/Cellar/python@3.9/3.9.5/Frameworks/P
ython.framework/Versions/3.9/lib/python3.9/asyncio/even
ts.py",line 80,in _run
    self._context.run(self._callback,*self._args)
  File "/usr/local/Cellar/python@3.9/3.9.5/Frameworks/P
ython.framework/Versions/3.9/lib/python3.9/asyncio/task
s.py",line 679,in ensure_future
    raise TypeError('An asyncio.Future,a coroutine or
an awaitable is '
TypeError: An asyncio.Future,a coroutine or an awaitab
le is required

我显然正在传递一个异步函数并将其转换为未来。我无法弄清楚我哪里出错了。我已经多次阅读 pyo3-async 的文档,但我仍然无法弄清楚。 我真的很感激一些帮助。

提前致谢。

解决方法

正如@sebpuetz 所建议的,

我所需要的只是 改变

s.start(h)

s.start(h())

因为 h 本身只是一个函数。 h() 是一个协程,这是必需的项目。