0

我正在尝试在 actix Web 套接字中执行 python 异步函数。

我能够弄清楚如何执行同步功能并将响应发送回网络套接字。但是,我无法对异步函数做同样的事情。

以下是我的方法:


            Ok(ws::Message::Text(text)) => {
                // need to also passs this text as a param
                let handler_function = &self.router.get("message").unwrap().0;
                let _number_of_params = &self.router.get("message").unwrap().1;
                println!("{:?}", handler_function);
                match handler_function {
                    PyFunction::SyncFunction(handler) => Python::with_gil(|py| {
                        let handler = handler.as_ref(py);
                        // call execute function
                        let op = handler.call0().unwrap();
                        let op: &str = op.extract().unwrap();

                        return ctx.text(op);
                        // return ctx.text(op);
                    }),
                    PyFunction::CoRoutine(handler) => {
                        let fut = Python::with_gil(|py| {
                            let handler = handler.as_ref(py);
                            Ok(pyo3_asyncio::tokio::into_future(handler.call0().unwrap()))
                        })
                        .unwrap();
                        let fut = actix::fut::wrap_future::<_, Self>(fut.unwrap());
                        ctx.spawn(fut);
                        return ctx.text("Async Functions are not supported in WS right now.");
                    }
                }
            }


我收到以下错误:

error[E0271]: type mismatch resolving `<FutureWrap<impl futures_util::Future+std::marker::Send, MyWs> as ActorFuture<MyWs>>::Output == ()`
  --> src/web_socket_connection.rs:88:29
   |
88 |                         ctx.spawn(fut);
   |                             ^^^^^ expected enum `Result`, found `()`
   |
   = note:   expected enum `Result<pyo3::Py<pyo3::PyAny>, pyo3::PyErr>`
           found unit type `()`


error: aborting due to previous error> ] 226/227: robyn

如果您需要我提供更多详细信息,请告诉我。

NEW_APPRROACH

我取得了一些进展并采用了一种新方法,将 python 函数包装在异步函数中。


            Ok(ws::Message::Text(text)) => {
                // need to also passs this text as a param
                let handler_function = &self.router.get("message").unwrap().0;
                let _number_of_params = &self.router.get("message").unwrap().1;
                println!("{:?}", handler_function);
                match handler_function {
                    PyFunction::SyncFunction(handler) => Python::with_gil(|py| {
                        let handler = handler.as_ref(py);
                        // call execute function
                        let op = handler.call0().unwrap();
                        let op: &str = op.extract().unwrap();

                        return ctx.text(op);
                        // return ctx.text(op);
                    }),
                    PyFunction::CoRoutine(handler) => {
                        println!("{:?}", handler);
                        println!("Async functions are not supported in WS right now.");
                        let fut = Python::with_gil(|py| {
                            let handler = handler.as_ref(py);
                            pyo3_asyncio::tokio::into_future(handler.call0().unwrap()).unwrap()
                        });
                        let f = async move {
                            let output = fut.await.unwrap();
                            Python::with_gil(|py| {
                                let contents: &str = output.extract(py).unwrap();
                                println!("{:?}", contents);
                            });
                        };
                        let fut = actix::fut::wrap_future::<_, Self>(f);
                        // fut.spawn(self);
                        ctx.spawn(fut);
                        return ctx.text("Async Functions are not supported in WS right now.");
                    }
                }
            }

但是现在,我收到了一个错误,即从未等待过协程

Async functions are not supported in WS right now.
thread 'actix-rt|system:0|arbiter:0' panicked at 'called `Result::unwrap()` on an `Err` value: PyErr { type: <class 'RuntimeError'>, value: RuntimeError('no running event loop'), traceback: None }', src/web_socket_connection.rs:85:88
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
sys:1: RuntimeWarning: coroutine 'connect' was never awaited
4

0 回答 0