1

I'm trying to create Actix Actor which has PyO3 Python interpreter & Py objects.

Question is what is the correct way to create python interpreter actor?

I think err caused by Actor trait defined static. https://docs.rs/actix/0.7.4/actix/trait.Actor.html

Is there the way of Actor or Context have object require life parameter?

rust version: nightly-2018-09-04, actix version: 0.7.4

This is current code.

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();
}

This code can't compile with this err.

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
2
  • Any reason for using version 0.5.1 instead of the newest (0.7.4)?
    – hellow
    Sep 12, 2018 at 6:44
  • thx notice ! I add version info. Sep 12, 2018 at 6:55

2 Answers 2

0

As Nikolay says, you can store Py<PyList> object in WsActor. To restore PyList, you can acquire GIL again and call .as_ref(python) method of AsPyRef trait (which Py<T> implements). An example is as follows:

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();
}
0
0

The definition of the Actor trait is

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

which means, your lifetime 'a must be 'static.

Here's a little example:

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() {}
}
6
  • Thanks for static lifetime explanation. Sep 12, 2018 at 7:10
  • This mean Actor can't have object require life parameter ? Sep 12, 2018 at 7:20
  • If so, is there any way to manage python interpreter in actix web ? Sep 12, 2018 at 7:22
  • I can't tell you for sure, because I haven't looked in pyo3. I think you have to figure that out by yourself :/
    – hellow
    Sep 12, 2018 at 7:33
  • 1
    You have to acquire gil inside actor’s handler. You can store python object references in actor as Py<T> or PyObject Sep 12, 2018 at 11:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.