Last active
December 10, 2020 01:35
-
-
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'
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| [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" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
change tokio version 0.2 to
0.3