Rust actix web处理程序内的HTTP请求->;一次有多个执行者:EnterError

Rust actix web处理程序内的HTTP请求->;一次有多个执行者:EnterError,rust,rust-tokio,hyper,actix-web,Rust,Rust Tokio,Hyper,Actix Web,在actix webresolver中创建hyper-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/res

actix web
resolver中创建hyper-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.
梅因

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")
}
用于触发错误的curl命令:

curl -X POST -H 'Content-Type: application/json' -d '{"test":1}' http://localhost:8080/push

Repo举例说明:

通过使用
tokio
功能
spawn
将未来添加到运行中的tokio执行器,使其正常工作

因此,不是:

System::new("test").block_on(lazy(|| {
使用:


当然,在
cargo.toml
中添加
tokio
作为依赖项,并包括板条箱。

这是因为actix web从1.0.0版开始使用tokio。正如Reqwest所做的那样,您最终会得到两个运行时


处理此问题的最佳方法之一是切换到处理程序和reqwest请求的异步版本。这个过程可能有点复杂,但从长远来看是值得的。在解释转换方面做得很好。

请提供更好的解释:最好是逐字复制编译器中的错误消息。为要编译的程序添加必要的
use
语句。了解所有依赖项的特定版本,或者至少了解actix web和reqwest可能是相关的。
[…]
中的任何内容都很重要,或者应该删除。提供了更多提示。@SirE\u net4theDownvoter请再次检查。现在已经添加了示例以及所有相关的附加细节。不幸的是,rust Playway似乎不提供
货物.toml
板条箱,否则我也会添加它,而是添加了一个指向github回购的链接。
System::new("test").block_on(lazy(|| {
spawn(lazy(move || {