What is the difference between tokio::spawn(my_future).await and just my_future.await?

One typically doesn’t await a spawned task (or at least not right away). It’s more common to simply write: tokio::spawn(my_future); Leave out the .await and the task will run in the background while the current task continues. Immediately calling .await blocks the current task. spawn(task).await is effectively no different than task.await. It’s akin to creating … Read more

Why do I get the error “there is no reactor running, must be called from the context of Tokio runtime” even though I have #[tokio::main]?

You are using a newer version of Tokio, such as 0.3 or 1.x, and many packages, including mdns 1.1.0, rely on an older version of Tokio, such as 0.2. % cargo tree -d tokio v0.2.22 └── mdns v1.1.0 └── example_project v0.1.0 tokio v0.3.3 └── example_project v0.1.0 For now, you will need to match versions of … Read more

How to test async functions that use Tokio?

Just replace #[test] with #[tokio::test] before any test function. If you use actix-web you can add actix_rt into Cargo.toml and #[actix_rt::test] before the test function #[tokio::test] async fn test_something_async() { let DB = setup().await; // <- the DB is impl std::future::Future type // the DB variable will be used to run another // async function … Read more

How can I perform parallel asynchronous HTTP GET requests with reqwest?

Concurrent requests As of reqwest 0.11.14: use futures::{stream, StreamExt}; // 0.3.27 use reqwest::Client; // 0.11.14 use tokio; // 1.26.0, features = [“macros”] const CONCURRENT_REQUESTS: usize = 2; #[tokio::main] async fn main() { let client = Client::new(); let urls = vec![“https://api.ipify.org”; 2]; let bodies = stream::iter(urls) .map(|url| { let client = &client; async move { let … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)