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
5
votes
1 answer

Is AsyncReadExt::read_u64 cancel safe?

In the documentation for AsyncReadExt::read_u64 it says it has the same errors as AsyncReadExt::read_exact, but says nothing about cancellation safety. The same holds for all the other read_ functions on AsyncReadExt. It seems likely that they…
AlbertGarde
  • 369
  • 1
  • 13
5
votes
2 answers

Awaiting a Number of Futures Unknown at Compile Time

I want to leverage Tokio's runtime to handle a variable amount of async futures. Since the count of futures is unknown at compile time, it seems FuturesUnordered is my best option (macros such as select! require specifying your branches at compile…
armani
  • 93
  • 1
  • 10
  • 23
5
votes
1 answer

How can I accept invalid or self-signed SSL certificates in Rust futures reqwest?

My code looks like the following: let fetches = futures::stream::iter( hosts.into_iter().map(|url| { async move { match reqwest::get(&url).await { // Ok and Err statements here! } But,…
Binit Ghimire
  • 53
  • 1
  • 5
5
votes
1 answer

What is the idiomatic way in Rust of assigning values of different types to a variable, depending on the compilation target OS?

I'm working on a codebase which binds to a Tokio socket and manages a TCP connection. In production, it binds to an AF_VSOCK using the tokio-vsock crate. While developing locally on Mac, the AF_VSOCK API isn't available as there is no hypervisor ->…
narruc
  • 350
  • 4
  • 9
5
votes
1 answer

Why do my tokio tasks quit before finishing?

The program below is supposed to print at regular intervals from multiple threads, but it is not working as I expected: # Cargo.toml [dependencies] tokio = { version = "0.3", features = ["full"] } use tokio::prelude::*; //0.3.4 use…
nkkr
  • 101
  • 5
5
votes
1 answer

Does the tokio task scheduler "steal work" or fetch from the global queue first?

In tokio, when the processors finish all the tasks in their run queue, do they first look into the global queue for more tasks or do they try work steal from sibling processors first?
pandawithcat
  • 571
  • 2
  • 13
5
votes
2 answers

How to find if tokio::sync::mpsc::Receiver has been closed?

I have a loop where I do some work and send result with Sender. The work takes time and I need to retry it in case of failure. It's possible that while I retry it, the receiver has been closed and my retries are going to be a waste of time. Because…
Artem Malinko
  • 1,761
  • 1
  • 22
  • 39
5
votes
0 answers

"borrowed value does not live long enough" when using tokio::spawn with a future with a mutable reference

The following code does not compile bceause the compiler can not assure hashmap_of_lists would live long enough. I can not overcome this. I've tried using Arc and Mutex but then I had other issues because of the async fashion of some_func and using…
Gal Ben David
  • 410
  • 4
  • 10
5
votes
0 answers

Write to websocket stream

I am connecting to a websocket server with tungstenite from this example and using write.send from there let connect_addr = env::args() .nth(1) .unwrap_or_else(|| panic!("this program requires at least one argument")); let url =…
Tarang
  • 75,157
  • 39
  • 215
  • 276
5
votes
3 answers

Why do asynchronous versions of a TCP echo server use 50x more memory than a synchronous one?

I have a simple TCP echo server using standard library: use std::net::TcpListener; fn main() { let listener = TcpListener::bind("localhost:4321").unwrap(); loop { let (conn, _addr) = listener.accept().unwrap(); …
Gurwinder Singh
  • 38,557
  • 6
  • 51
  • 76
5
votes
2 answers

How do I use TcpStream split across 2 threads with async?

I am trying to use the read and write of a tcp stream in different threads. This is what I currently have: use tokio::prelude::*; use tokio::net::TcpStream; #[tokio::main] async fn main() -> Result<(), Box> { let mut…
Gus
  • 151
  • 2
  • 15
5
votes
1 answer

Await for future again after tokio::time::timeout

Background: I have a process using tokio::process to spawn child processes with handles in the tokio runtime. It is also responsible for freeing the resources after killing a child and, according to the documentation (std::process::Child,…
mr.celo
  • 585
  • 1
  • 5
  • 17
5
votes
1 answer

Return Future value from a function

I recently started to learn Rust and I'm not sure how I can return future value from a function that should return a Result. When I try to return just the response variable and remove the Result output, I get an error: cannot use the ? operator in a…
zorro
  • 2,305
  • 2
  • 11
  • 14
5
votes
1 answer

Why does tokio::spawn complain about lifetimes even with .clone()?

I am trying to compile the following seemingly straightforward code, but I'm getting an error: use std::io::Error; #[derive(Debug)] struct NetworkConfig { bind: String, node_key_file: String, } async fn network_handler(network_config:…
dmp32
  • 95
  • 2
  • 5
5
votes
2 answers

How to convert a Future into a Stream?

I'm trying to use async_std to receive UDP datagrams from the network. There is a UdpSocket that implements async recv_from, this method returns a future but I need a async_std::stream::Stream that gives a stream of UDP datagrams because it is a…
Superuzir
  • 323
  • 2
  • 5