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
18
votes
3 answers

How do I read the entire body of a Tokio-based Hyper request?

I want to write a server using the current master branch of Hyper that saves a message that is delivered by a POST request and sends this message to every incoming GET request. I have this, mostly copied from the Hyper examples directory: extern…
JayStrictor
  • 468
  • 1
  • 4
  • 10
17
votes
2 answers

How can I use Tokio to trigger a function every period or interval in seconds?

In Node.js I can set the interval that a certain event should be triggered, function intervalFunc() { console.log('whelp, triggered again!'); } setInterval(intervalFunc, 1500); However the interface for Tokio's interval is a bit more complex. It…
Evan Carroll
  • 78,363
  • 46
  • 261
  • 468
16
votes
4 answers

How to create a dedicated threadpool for CPU-intensive work in Tokio?

I have a Rust async server based on the Tokio runtime. It has to process a mix of latency-sensitive I/O-bound requests, and heavy CPU-bound requests. I don't want to let the CPU-bound tasks monopolize the Tokio runtime and starve the I/O bound…
Kornel
  • 97,764
  • 37
  • 219
  • 309
16
votes
0 answers

Forwarding from a futures::Stream to a futures::Sink

I am currently trying to wrap my head around the tokio & futures primitives and ecosystem. I started doing some work from the tk-http websockets example, and wanted to do more processing on the received data rather than echoing it back. A first step…
remram
  • 4,805
  • 1
  • 29
  • 42
15
votes
2 answers

What is the difference between futures::select! and tokio::select?

I'm using Tokio and I want to receive requests from two different mpsc queues. select! seems like the way to go, but I'm not sure what the difference is between futures::select! and tokio::select!. Under which circumstances one should you use one…
ynimous
  • 4,642
  • 6
  • 27
  • 43
14
votes
2 answers

Why does Tokio return the error "Cannot drop a runtime in a context where blocking is not allowed"?

I have a Tokio client that talks to a remote server and is supposed to keep the connection alive permanently. I've implemented the initial authentication handshake and found that when my test terminates, I get an odd panic: ----…
ruipacheco
  • 15,025
  • 19
  • 82
  • 138
14
votes
1 answer

How can I spawn asynchronous methods in a loop?

I have a vector of objects that have a resolve() method that uses reqwest to query an external web API. After I call the resolve() method on each object, I want to print the result of every request. Here's my half-asynchronous code that compiles and…
Djent
  • 2,877
  • 10
  • 41
  • 66
13
votes
1 answer

How to remotely shut down running tasks with Tokio

I have a UDP socket that is receiving data pub async fn start() -> Result<(), std::io::Error> { loop { let mut data = vec![0; 1024]; socket.recv_from(&mut data).await?; } } This code is currently blocked on the .await when…
misnomer___
  • 355
  • 1
  • 3
  • 8
12
votes
2 answers

tokio::select! but for a Vec of futures

I have a Vec of futures which I want to execute concurrently (but not necessarily in parallel). Basically, I'm looking for some kind of select function that is similar to tokio::select! but takes a collection of futures, or, conversely, a function…
Florian Brucker
  • 9,621
  • 3
  • 48
  • 81
11
votes
1 answer

What is the difference between std::sync::Mutex vs tokio::sync::Mutex?

What is an "async" mutex as opposed to a "normal" mutex? I believe this is the difference between tokio's Mutex and the normal std lib Mutex. But I don't get, conceptually, how a mutex can be "async". Isn't the whole point that only one thing can…
Test
  • 962
  • 9
  • 26
11
votes
2 answers

Cannot borrow data in an `Arc` as mutable

I don't know what to do next. It looks like I misunderstand something, or maybe I have not learned some critical topic. use std::sync::Arc; use reqwest::{Error, Response}; // 0.11.4 use tokio::sync::mpsc::{self, Receiver, Sender}; // 1.9.0 pub…
Lex
  • 194
  • 1
  • 2
  • 11
11
votes
1 answer

Using Actix from a Tokio App: mixing actix_web::main and tokio::main?

Currently I have a main written like the async example for the Reqwest library. #[tokio::main] async fn main() -> Result<(), Box> { We can use the exact example there for this. Now I want to basically add a -l flag to…
Evan Carroll
  • 78,363
  • 46
  • 261
  • 468
11
votes
2 answers

The trait bound `(): futures::Future` is not satisfied when using TcpConnectionNew

I am trying to write a simple TCP client in Rust using Tokio crate. My code is pretty close to this example minus the TLS: extern crate futures; extern crate tokio_core; extern crate tokio_io; use futures::Future; use…
Sergey
  • 1,166
  • 14
  • 27
10
votes
1 answer

What is the difference between tokio::spawn and tokio::task::spawn in Rust?

I am making a web server capable of async operation using tokio. I created a task via tokio::spawn, and I saw tokio::task::spawn working as well. What is the difference between tokio::sapwn and tokio::task::spawn?
yjlee
  • 363
  • 1
  • 14
10
votes
1 answer

reqwest::Error { kind: Decode, source: Error("expected value", line: 1, column: 1) }'

i get the following error while creating this POST request. Im a newbie in RUST. Instead of serde_json::Value I have even tried HashMap still the same issue. if you could tell me if my headers are wrong or how do I trace if its…
STEEL
  • 8,955
  • 9
  • 67
  • 89
1
2
3
70 71