我正在尝试创建具有 PyO3 Python 解释器和 Py 对象的 Actix Actor。
问题是创建python解释器actor的正确方法是什么?
我认为由 Actor trait 定义的静态引起的错误。 https://docs.rs/actix/0.7.4/actix/trait.Actor.html
有没有 Actor 或 Context 的方式让对象需要生命参数?
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();
}
此代码无法编译此错误。
error[E0478]: lifetime bound not satisfied
--> src/main.rs:15:10
|
15 | impl<'a> Actor for WsActor<'a> {
| ^^^^^
|
note: lifetime parameter instantiated with the lifetime 'a as defined on the impl at 15:6
--> src/main.rs:15:6
|
15 | impl<'a> Actor for WsActor<'a> {
| ^^
= note: but lifetime parameter must outlive the static lifetime