繁体   English   中英

如何从 Rust 调用 Python async function?

[英]How to call Python async function from Rust?

我已经阅读了这个答案( How to call Rust async method from Python? ),而我想做的是与此相反。

我想调用 Python async function 并在 tokio 的运行时等待它。 可能吗?

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

这就是我的 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

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

提前致谢。

正如@sebpuetz 所建议的,

我所需要的只是改变

s.start(h)

s.start(h())

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM