Questions tagged [rust-tokio]

Tokio is an event-driven, non-blocking I/O platform for writing asynchronous applications with the Rust programming language.

Tokio aims to be:

  • Fast: Tokio's zero-cost abstractions give you bare-metal performance.

  • Productive: Tokio makes it easy to implement protocols and program asynchronously.

  • Reliable: Tokio leverages Rust's ownership and concurrency model to ensure thread safety.

  • Scalable: Tokio has a minimal footprint, and handles backpressure and cancellation naturally.

At a high level, it provides a few major components:

  • A multithreaded, work-stealing based task scheduler.
  • A reactor backed by the operating system's event queue (epoll, kqueue, IOCP, etc...).
  • Asynchronous TCP and UDP sockets.

You can find more information in

1053 questions
65
votes
1 answer

How to test async functions that use Tokio?

I have an async function that I need to test. This function uses a mongodb::Database object to run, so I initialize the connection in the setup() function and use tokio_test::block_on() to wrap the await expression inside. #[cfg(test)] mod tests { …
DennyHiu
  • 4,861
  • 8
  • 48
  • 80
56
votes
2 answers

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

The async example is useful, but being new to Rust and Tokio, I am struggling to work out how to do N requests at once, using URLs from a vector, and creating an iterator of the response HTML for each URL as a string. How could this be done?
user964375
  • 2,201
  • 3
  • 26
  • 27
36
votes
3 answers

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]?

I'm following the mdns Rust documentation and pasted the example code but it throws the following error: thread 'main' panicked at 'there is no reactor running, must be called from the context of Tokio runtime' Here's the code that I have: use…
o.o
  • 3,563
  • 9
  • 42
  • 71
34
votes
1 answer

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

Given an async function and it's corresponding future, lets say: async fn foo() -> Result { // ... } let my_future = foo(); what is the difference between awaiting it using just .await other than using…
hbobenicio
  • 1,000
  • 15
  • 16
33
votes
2 answers

Cannot find tokio::main macro?

I am creating a sample Rust project in my Windows system to download a file by HTTP GET request in async mode. My code is as follows (same as the code mentioned in the Rust Cookbook): extern crate error_chain; extern crate tempfile; extern crate…
user14200418
24
votes
3 answers

When should you use tokio::join!() over tokio::spawn()?

Let's say I want to download two web pages concurrently with Tokio... Either I could implement this with tokio::spawn(): async fn v1() { let t1 = tokio::spawn(reqwest::get("https://example.com")); let t2 =…
24
votes
1 answer

Asynchronously reconnecting a client to a server in an infinite loop

I'm not able to create a client that tries to connect to a server and: if the server is down it has to try again in an infinite loop if the server is up and connection is successful, when the connection is lost (i.e. server disconnects the client)…
Danilo Silva
  • 293
  • 2
  • 6
23
votes
1 answer

How do I await a future inside a non-async method which was called from an async method within the context of a Tokio runtime?

I'm using Tokio 1.1 to do async things. I have an async main with #[tokio::main] so I'm already operating with a runtime. main invokes a non-async method where I'd like to be await on a future (specifically, I'm collecting from a datafusion…
growse
  • 3,554
  • 9
  • 43
  • 66
22
votes
3 answers

How can I define an async method in a trait?

I have a trait that I'm using to abstract away tokio::net::TcpStream and tokio::net::UnixStream: /// Interface for TcpStream and UnixStream. trait TryRead { // overlapping the name makes it hard to work with fn do_try_read(&self, buf: &mut [u8])…
ruipacheco
  • 15,025
  • 19
  • 82
  • 138
22
votes
3 answers

How to tokio::join multiple tasks?

Imagine that some futures are stored in a Vec whose length are runtime-determined, you are supposed to join these futures concurrently, what should you do? Obviously, by the example in the document of tokio::join, manually specifying each length the…
bruceyuan
  • 421
  • 1
  • 3
  • 8
21
votes
2 answers

How can I create a Tokio runtime inside another Tokio runtime without getting the error "Cannot start a runtime from within a runtime"?

I'm using rust_bert for summarising text. I need to set a model with rust_bert::pipelines::summarization::SummarizationModel::new, which fetches the model from the internet. It does this asynchronously using tokio and the issue that (I think) I'm…
Mib
  • 321
  • 1
  • 2
  • 4
20
votes
2 answers

Why do I get "panicked at 'not currently running on the Tokio runtime'" when using block_on from the futures crate?

I'm using the example code on elastic search's blog post about their new crate and I'm unable to get it working as intended. The thread panics with thread 'main' panicked at 'not currently running on the Tokio runtime.'. What is the Tokio runtime,…
Miranda Abbasi
  • 521
  • 2
  • 5
  • 10
19
votes
2 answers

Rust Async Drop

I'm facing a scenario where I need to run async code from the drop handler of an object. The whole application runs in a tokio async context, so I know that the drop handler is called with an active tokio Runtime, but unfortunately drop itself is a…
Heinzi
  • 5,793
  • 4
  • 40
  • 69
18
votes
2 answers

How to run an asynchronous task from a non-main thread in Tokio?

use std::thread; use tokio::task; // 0.3.4 #[tokio::main] async fn main() { thread::spawn(|| { task::spawn(async { println!("123"); }); }) .join(); } When compiling I get a warning: warning: unused…
ibse
  • 439
  • 1
  • 4
  • 13
18
votes
2 answers

How do I gracefully shutdown the Tokio runtime in response to a SIGTERM?

I have a main function, where I create a Tokio runtime and run two futures on it. use tokio; fn main() { let mut runtime = tokio::runtime::Runtime::new().unwrap(); runtime.spawn(MyMegaFutureNumberOne {}); …
hedgar2017
  • 1,425
  • 3
  • 21
  • 40
1
2 3
70 71