Skip to content

Instantly share code, notes, and snippets.

@smallfish
Last active December 10, 2020 01:35
Show Gist options
  • Select an option

  • Save smallfish/98cc4d42451d7111e5aa672c04c153b9 to your computer and use it in GitHub Desktop.

Select an option

Save smallfish/98cc4d42451d7111e5aa672c04c153b9 to your computer and use it in GitHub Desktop.
tokio-0.2-0.3 thread panicked at 'there is no reactor running, must be called from the context of Tokio runtime'
[package]
name = "hello-tokio"
version = "0.1.0"
authors = ["smallfish"]
edition = "2018"
[dependencies]
tokio = { version = "0.2", features = ["full"] }
#tokio = { version = "0.3", features = ["full"] }
actix-web = "3.0.2"
reqwest = { version = "0.10", features = ["json"] }
thiserror = "1.0"
log = "0.4"
env_logger = "0.8"
use actix_web::{web, App, HttpServer, Responder};
use reqwest;
use std::thread;
use std::thread::sleep;
use thiserror::Error;
use tokio::time::Duration;
const ADDR: &str = "0.0.0.0:8081";
#[derive(Error, Debug)]
enum MyError {
#[error("reqwest error")]
ReqwestError(#[from] reqwest::Error),
}
async fn index() -> impl Responder {
"index"
}
async fn check_health(url: &str) -> Result<u16, MyError> {
let client = reqwest::Client::new().get(url).send().await?;
Ok(client.status().as_u16())
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
env_logger::init();
let url = format!("http://{}", ADDR);
thread::spawn(move || {
let mut rt = tokio::runtime::Runtime::new().unwrap();
loop {
sleep(Duration::from_secs(3));
let result = rt.block_on(check_health(url.as_str()));
log::info!("check {:?} health {:?}", url, result);
}
});
HttpServer::new(|| App::new().route("/", web::get().to(index)))
.bind(ADDR)?
.run()
.await
}
@smallfish
Copy link
Author

change tokio version 0.2 to 0.3

$ RUST_BACKTRACE=1 RUST_LOG=info cargo run
...
thread '<unnamed>' panicked at 'there is no reactor running, must be called from the context of Tokio runtime'..
stack backtrace:
   0: rust_begin_unwind
...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment