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
4
votes
0 answers

Axum does no redirect when being in Root Route when redirecting from / and with routing normalization

I am trying to make a multilingual website that redirects you to the homepage in the corresponding language when accessing it's / route, but for some reason Axum is no being able to redirect, but only when setting the redirection from the / route…
Axum500
  • 41
  • 2
4
votes
1 answer

rust receive messages from multiple channels at once

I have a Vec> (basically a vector of channel receivers). I want to subscribe to all of them and get messages from all of them. How can I do it?
4
votes
1 answer

Why does `tokio::spawn` requires a `'static` lifetime if I immediately await it?

I want to run two Future in parallel, and if possible in different threads : try_join!( tokio::spawn(fut1), // fut1 is not 'static tokio::spawn(fut2) )?; If my understanding is correct, tokio::spawn requires a Future to be 'static because the…
Léo Coletta
  • 1,099
  • 2
  • 12
  • 24
4
votes
1 answer

When should you use Tokio's `spawn_blocking`?

In the task documentation, there's a section that talks about calling blocking code in async, and how that should be avoided as to not block the async thread too much (https://docs.rs/tokio/1.21.2/tokio/task/index.html#blocking-and-yielding). It…
boston
  • 95
  • 1
  • 7
4
votes
0 answers

How to implement tokio_postgres::types::ToSql for enum type (rust, postgres)

I need to implement the tokio_postgres::types::ToSql for an enum type (rust and db as enum implemented) and I had no idea how to ... Example enum Flag {MyFlag1, MyFlag2, MyFlag3}; // on postgres db : // CREATE TYPE flag AS ENUM ('my_flag_1',…
Siassei
  • 41
  • 1
4
votes
1 answer

Tokio select macro conditions and arm evaluation

I have a code that needs to be read from several TcpStream. One of these TcpStream will always be there, but another may or may not. I wrote the code before and (naively) used conditions on tokio select macro. Unfortunately, I quickly learned that…
Victor Ronin
  • 22,758
  • 18
  • 92
  • 184
4
votes
1 answer

How to choose between block_in_place and spawn_blocking?

I'm working a lot with tokio and I've been using spawn_blocking for code that is going to block the thread. Then I saw the documentation for block_in_place and it seems like it's an unrestricted (Send, 'static) version of the former. My question is,…
lsunsi
  • 41
  • 3
4
votes
1 answer

How to test a method with an asynchronous infinite loop?

I have a struct that works as a postmaster for a server application: since I don't know how many clients will connect I have the postmaster listen on a socket and start a new struct with business logic whenever a client opens a connection. But this…
ruipacheco
  • 15,025
  • 19
  • 82
  • 138
4
votes
0 answers

`TcpStream::connect` freezes `tokio` for one second

Consider the following snippet: use std::net::Ipv4Addr; use std::time::{Duration, Instant}; use tokio::net::{TcpListener, TcpStream}; use tokio::time; #[tokio::main(flavor = "current_thread")] async fn main() { tokio::spawn(async { …
Matteo Monti
  • 8,362
  • 19
  • 68
  • 114
4
votes
1 answer

tokio::try_join! doesn't return the Err variant when one of the tasks returns Err?

I'm having trouble understanding the interaction between tokio::try_run! and tasks running inside tokio::spawn returning an Err. When I run the following sample: use tokio::time::{sleep, Duration}; #[tokio::main] async fn main() { let h1 =…
jgpaiva
  • 369
  • 2
  • 11
4
votes
2 answers

Can I store an `impl Future` as a concrete type?

tokio::net::TcpStream::connect is an async function, meaning it returns an existential type, impl Future. I would like to store a Vec of these futures in a struct. I've found many questions where someone wants to store multiple different impl…
Ibraheem Ahmed
  • 11,652
  • 2
  • 48
  • 54
4
votes
1 answer

Unexpected tokio::task::spawn_blocking behavior

I'm experimenting with tokio's tokio::spawn and tokio::task::spawn and turns out I don't understand how the latter behaves. When I run the following code: #[tokio::main] pub async fn main() { // I'm spawning one block of functions let h =…
ilmoi
  • 1,994
  • 2
  • 21
  • 45
4
votes
1 answer

rust tokio trait bounds were not satisfied on forward method

I upgraded wrap and tokio in my rust project and after the upgrade, the forward method got an error. I searched through the documentation but there was no forward method in the new version of tokio framework. Error error[E0599]: the method `forward`…
Hasan Parasteh
  • 431
  • 8
  • 25
4
votes
2 answers

How do I spawn a long running Tokio task within another task without blocking the parent task?

I'm trying to construct an object that can manage a feed from a websocket but be able to switch between multiple feeds. There is a Feed trait: trait Feed { async fn start(&mut self); async fn stop(&mut self); } There are three structs that…
John Cantrell
  • 51
  • 1
  • 3
4
votes
2 answers

Is tokio multithreaded?

I know tokio allows to write concurrent code. But I'm not sure if it runs in parallel. My computer has eight cores. So ideally I would run no more than eight threads. If I needed more concurrency I would run coroutines on top of those threads (using…
Tomás Vallotton
  • 538
  • 6
  • 13