我正在尝试创建Actix Actor,它有PyO3 Python解释器和Py对象。在
问题是创建python解释器actor的正确方法是什么?在
我认为错误是由演员特质定义的静态的。 https://docs.rs/actix/0.7.4/actix/trait.Actor.html
有没有参与者或上下文有对象需要生命参数的方式?在
rust版本:nightly-2018-09-04,actix版本:0.7.4
这是当前代码。在
extern crate actix;
extern crate actix_web;
extern crate pyo3;
use actix::prelude::*;
use actix_web::{http, server, ws, App, HttpRequest, HttpResponse, Error};
use pyo3::{Python, GILGuard, PyList};
struct WsActor<'a> {
// addr: Addr<PyActor>,
gil: GILGuard,
python: Python<'a>,
pylist: &'a PyList,
}
impl<'a> Actor for WsActor<'a> {
type Context = ws::WebsocketContext<Self>;
}
fn attach_ws_actor(req: &HttpRequest<()>) -> Result<HttpResponse, Error> {
let gil = Python::acquire_gil();
let python = gil.python();
let pylist = PyList::empty(python);
let actor = WsActor {gil, python, pylist};
ws::start(req, actor)
}
fn main() {
let sys = actix::System::new("example");
server::new(move || {
App::new()
.resource("/ws/", |r| r.method(http::Method::GET).f(attach_ws_actor))
}).bind("0.0.0.0:9999")
.unwrap()
.start();
}
此代码无法使用此错误进行编译。在
^{pr2}$
Actor trait的定义是
也就是说,你的生命周期
'a
必须是'static
。在这里有一个小example:
^{pr2}$正如Nikolay所说,您可以将
Py<PyList>
对象存储在WsActor
中。 为了恢复PyList
,您可以再次获得GIL并调用AsPyRef
trait的.as_ref(python)
方法(这是Py<T>
实现的)。 示例如下:相关问题 更多 >
编程相关推荐