elastic / elasticsearch-rs

Official Elasticsearch Rust Client
https://www.elastic.co/guide/en/elasticsearch/client/rust-api/current/index.html
Apache License 2.0
695 stars 70 forks source link

[BUG] there is no reactor running, must be called from the context of a Tokio 1.x runtime #207

Closed duguying closed 1 year ago

duguying commented 1 year ago

Describe the bug running elasticsearch-rs with block_on failure.

To Reproduce Steps to reproduce the behavior:

source as follow

Cargo.toml

[package]
name = "tes"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
serde_json = "1.0.85"
elasticsearch = { version = "8.4.0-alpha.1" }
futures = "0.3"
tokio = { version = "*", features = ["full"] }

main.rs

use elasticsearch::{http::transport::Transport, BulkParts, Elasticsearch};
use futures::executor::block_on;
use serde_json::Value;

fn main() {
    println!("Hello, world!");
    block_on(send_es("hi".to_string()));
    println!("hi");
}

pub async fn send_es(body: String) {
    let transport = Transport::single_node("http://xxx.xxx.net:19200").unwrap();
    let client = Elasticsearch::new(transport);
    let mut bodies: Vec<String> = Vec::with_capacity(1);
    bodies.push(body);
    let response = client
        .bulk(BulkParts::Index("nginx"))
        .body(bodies)
        .send()
        .await
        .unwrap();
    let response_body = response.json::<Value>().await.unwrap();
    println!("{:?}", response_body);
}

running failure

     Running `target/debug/tes`
Hello, world!
thread 'main' panicked at 'there is no reactor running, must be called from the context of a Tokio 1.x runtime', /root/.cargo/registry/src/github.com-1ecc6299db9ec823/hyper-0.14.20/src/client/connect/dns.rs:121:24
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

env

➜  tes git:(master) ✗ cargo version
cargo 1.62.1 (a748cf5a3 2022-06-08)
duguying commented 1 year ago

use #[tokio::main] for calling async function.

dkvasnicka commented 7 months ago

How can I use the ES client in a program that I specifically do not want to use Tokio? This library is dragging Tokio in via reqwest and is messing up the operation of headless_chrome crate which is "smart" and tries to do async stuff if it senses Tokio around (= dies on context absence like described above). I just have a program that I want to be 100% linear and synchornous and I have my valid reasons for it -- how can I work with Elastic in that program?