actix-web 处理程序中的 HTTP 请求 -> 一次有多个执行程序:EnterError
HTTP request inside actix-web handler -> Multiple executors at once: EnterError
在 actix-web
解析器中创建超级 post 请求时,会抛出以下错误 - 如何通过将请求生成到现有执行程序来发送一个 http 请求?
thread 'actix-rt:worker:1' panicked at 'Multiple executors at once: EnterError { reason: "attempted to run an executor while another executor is already running" }', src/libcore/result.rs:999:5
note: Run with `RUST_BACKTRACE=1` environment variable to display a backtrace.
Panic in Arbiter thread, shutting down system.
main.rs
extern crate actix_web;
extern crate serde_json;
extern crate actix_rt;
extern crate hyper;
use serde_json::{Value, json};
use hyper::{Client, Uri, Body, Request};
use actix_web::{middleware, web, App, HttpResponse, HttpServer};
use actix_rt::System;
use actix_web::client;
use futures::future::{Future, lazy};
fn main() {
println!("Start server...");
listen();
}
pub fn listen() -> std::io::Result<()> {
HttpServer::new(|| {
App::new()
.wrap(middleware::Logger::default())
.data(web::JsonConfig::default().limit(4096))
.service(web::resource("/push").route(web::post().to(index)))
.service(web::resource("/test").route(web::post().to(test)))
})
.bind("127.0.0.1:8080")?
.run()
}
fn index(item: web::Json<Value>) -> HttpResponse {
println!("model: {:?}", &item);
send(json!({
"hello": "world"
}));
HttpResponse::Ok().json(item.0) // <- send response
}
fn test(item: web::Json<Value>) -> HttpResponse {
println!("recevied test call!");
println!("{:?}", &item);
HttpResponse::Ok().json(item.0) // <- send response
}
pub fn send(mut data: serde_json::Value) {
println!("# Start running log post future...");
// if the following line is removed, the call is not received by the test function above
System::new("test").block_on(lazy(|| {
let req = Request::builder()
.method("POST")
.uri("http://localhost:8080/test")
.body(Body::from(data.to_string()))
.expect("request builder");
let client = Client::new();
let future = client.request(req)
.and_then(|res| {
println!("status: {}", res.status());
Ok(())
})
.map_err(|err| {
println!("error: {}", err);
});
return future;
}));
println!("# Finish running log post future")
}
cargo.toml
[package]
name = "rust-tokio-event-loop-madness"
version = "0.1.0"
authors = [""]
edition = "2018"
[dependencies]
serde_json = "1.0.39"
actix-web = "1.0.0"
serde_derive = "1.0.92"
actix-rt = "*"
hyper = "0.12.30"
futures = "*"
curl 命令触发错误:
curl -X POST -H 'Content-Type: application/json' -d '{"test":1}' http://localhost:8080/push
回购示例:https://github.com/fabifrank/rust-tokio-event-loop-madness
通过使用 tokio
函数 spawn
将 future 添加到 tokio 的 运行 执行器使其工作。
所以代替:
System::new("test").block_on(lazy(|| {
使用:
spawn(lazy(move || {
当然还要在 cargo.toml
中添加 tokio
作为依赖项并包含 crate。
这是因为 actix-web 从 1.0.0 版本开始使用 Tokio。正如 Reqwest 所做的那样,您最终得到两个 运行 次。
处理此问题的最佳方法之一是切换到您的处理程序和 reqwest 请求的异步版本。这个过程可能有点复杂,但在漫长的 运行 中是值得的。 This article 很好地解释了过渡。
在 actix-web
解析器中创建超级 post 请求时,会抛出以下错误 - 如何通过将请求生成到现有执行程序来发送一个 http 请求?
thread 'actix-rt:worker:1' panicked at 'Multiple executors at once: EnterError { reason: "attempted to run an executor while another executor is already running" }', src/libcore/result.rs:999:5
note: Run with `RUST_BACKTRACE=1` environment variable to display a backtrace.
Panic in Arbiter thread, shutting down system.
main.rs
extern crate actix_web;
extern crate serde_json;
extern crate actix_rt;
extern crate hyper;
use serde_json::{Value, json};
use hyper::{Client, Uri, Body, Request};
use actix_web::{middleware, web, App, HttpResponse, HttpServer};
use actix_rt::System;
use actix_web::client;
use futures::future::{Future, lazy};
fn main() {
println!("Start server...");
listen();
}
pub fn listen() -> std::io::Result<()> {
HttpServer::new(|| {
App::new()
.wrap(middleware::Logger::default())
.data(web::JsonConfig::default().limit(4096))
.service(web::resource("/push").route(web::post().to(index)))
.service(web::resource("/test").route(web::post().to(test)))
})
.bind("127.0.0.1:8080")?
.run()
}
fn index(item: web::Json<Value>) -> HttpResponse {
println!("model: {:?}", &item);
send(json!({
"hello": "world"
}));
HttpResponse::Ok().json(item.0) // <- send response
}
fn test(item: web::Json<Value>) -> HttpResponse {
println!("recevied test call!");
println!("{:?}", &item);
HttpResponse::Ok().json(item.0) // <- send response
}
pub fn send(mut data: serde_json::Value) {
println!("# Start running log post future...");
// if the following line is removed, the call is not received by the test function above
System::new("test").block_on(lazy(|| {
let req = Request::builder()
.method("POST")
.uri("http://localhost:8080/test")
.body(Body::from(data.to_string()))
.expect("request builder");
let client = Client::new();
let future = client.request(req)
.and_then(|res| {
println!("status: {}", res.status());
Ok(())
})
.map_err(|err| {
println!("error: {}", err);
});
return future;
}));
println!("# Finish running log post future")
}
cargo.toml
[package]
name = "rust-tokio-event-loop-madness"
version = "0.1.0"
authors = [""]
edition = "2018"
[dependencies]
serde_json = "1.0.39"
actix-web = "1.0.0"
serde_derive = "1.0.92"
actix-rt = "*"
hyper = "0.12.30"
futures = "*"
curl 命令触发错误:
curl -X POST -H 'Content-Type: application/json' -d '{"test":1}' http://localhost:8080/push
回购示例:https://github.com/fabifrank/rust-tokio-event-loop-madness
通过使用 tokio
函数 spawn
将 future 添加到 tokio 的 运行 执行器使其工作。
所以代替:
System::new("test").block_on(lazy(|| {
使用:
spawn(lazy(move || {
当然还要在 cargo.toml
中添加 tokio
作为依赖项并包含 crate。
这是因为 actix-web 从 1.0.0 版本开始使用 Tokio。正如 Reqwest 所做的那样,您最终得到两个 运行 次。
处理此问题的最佳方法之一是切换到您的处理程序和 reqwest 请求的异步版本。这个过程可能有点复杂,但在漫长的 运行 中是值得的。 This article 很好地解释了过渡。