首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >actix如何拥有PyO3 Python?

actix如何拥有PyO3 Python?
EN

Stack Overflow用户
提问于 2018-09-12 06:13:47
回答 2查看 577关注 0票数 1

我正在尝试创建Actix,它具有PyO3解释器& Py对象。

问题是,创建python解释器参与者的正确方法是什么?

我认为错误是由演员的特质定义为静态的。https://docs.rs/actix/0.7.4/actix/trait.Actor.html

是否有行为者或上下文中有对象要求生命参数的方式?

锈蚀版本:夜间-2018-09-04,actix版本: 0.7.4

这是当前代码。

代码语言:javascript
复制
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();
}

这段代码不能用这个错误编译。

代码语言:javascript
复制
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
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-09-17 10:14:38

正如尼古拉所说,您可以将Py<PyList>对象存储在WsActor中。要恢复PyList,您可以再次获取GIL并调用AsPyRef特性的.as_ref(python)方法( Py<T>实现该方法)。一个例子如下:

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2018-09-12 06:54:23

演员特质的定义是

代码语言:javascript
复制
pub trait Actor: Sized + 'static { ... }

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

这里有一个小示例

代码语言:javascript
复制
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() {}
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52288565

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档