1

我正在尝试创建具有 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
4

2 回答 2

0

Actor 特征的定义是

pub trait Actor: Sized + 'static { ... }

这意味着,你的一生'a必须是'static

这是一个小例子

use std::marker::PhantomData;

trait Foo: Sized + 'static {
    fn foo();
}

struct Bar<'a> {
    _marker: PhantomData<&'a i32>,
}
impl<'a> Foo for Bar<'a> { //not possible
    fn foo() {}
}

struct Baz<'a> {
    _marker: PhantomData<&'a i32>,
}
impl Foo for Baz<'static> { //possible
    fn foo() {}
}
于 2018-09-12T06:54:23.857 回答
0

正如Nikolay所说,您可以将Py<PyList>对象存储在WsActor. 要恢复PyList,您可以再次获取 GIL 并调用trait.as_ref(python)方法(实现)。一个例子如下:AsPyRefPy<T>

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, PyList, Py, AsPyRef};

struct WsActor {
    // addr: Addr<PyActor>,
    pylist: Py<PyList>,
}
impl Actor for WsActor {
    type Context = ws::WebsocketContext<Self>;
}
impl StreamHandler<ws::Message, ws::ProtocolError> for WsActor {
    fn handle(&mut self, _: ws::Message, _: &mut Self::Context) {
        let gil = Python::acquire_gil();
        let python = gil.python();
        let list = self.pylist.as_ref(python);
        println!("{}", list.len());
    }
}

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 {
        pylist: pylist.into()
    };
    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();
}
于 2018-09-17T10:14:38.133 回答