rust-actix
尝试从actix-web运行执行元时,spawn_local出现异常
我正在尝试在actix-web服务器中启动一个参与者,并使用数据传递其地址。
use actix::prelude::*;
use actix_web::{web, App, HttpRequest, HttpServer, Responder};
struct MyActor;
impl Actor for MyActor {
type Context = Context<Self>;
}
struct AppState {
actor_addr: Addr<MyActor>,
}
async fn greet(req: HttpRequest, data: web::Data<AppState>) -> impl Responder {
format!("Hello!")
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
let addr = MyActor.start();
HttpServer::new(move || {
App::new()
.data(AppState {
actor_addr: addr.clone(),
})
.route("/", web::get().to(greet))
})
.bind(("127.0.0.1", 8080))?
.run()
.await
}这个想法是从greet函数向参与者发送一条消息。在尝试运行此命令时,我遇到以下死机:
thread 'main' panicked at '`spawn_local` called from outside of a `task::LocalSet`', /Users/myuser/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.12.0/src/task/local.rs:305:18我尝试用#[actix_web::main]代替#[actix_rt::main],它启动了执行元。在隐藏代码时,我意识到#[actix_web::main]是#[actix_rt::main]的再导出,所以它应该可以工作。依赖项版本为:
actix-web = "3.3.2"
actix = "0.12.0"可能在依赖项中存在不匹配。
这是实现我正在尝试的目标的方式吗?
提前谢谢。
发布于 2021-10-17 17:23:26
更改为actix = "0.10",actix 0.12似乎有很多与actix-web 3.3冲突的更新依赖项。
https://stackoverflow.com/questions/69515530
复制相似问题