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
10
votes
2 answers

Is it possible to close a TcpListener in Tokio?

I have a tokio_core::net::TcpListener that I create and then call the incoming method on to get a stream of incoming connections. I then use the for_each method on that stream to turn it into a future and run the future on an event loop. Once I do…
sdlkjslfie
  • 135
  • 1
  • 5
9
votes
2 answers

Is there any point in async file IO?

Async runtimes for Rust like tokio provide "asyncified" copies of many standard functions, including some file IO ones, which work by basically just summoning the corresponding blocking task (on a new thread?). Examples of such functions are…
9
votes
1 answer

What is the recommended way to propagate panics in tokio tasks?

Right now my panics are being swallowed. In my use case, I would like it to crash entire program and also print the stack trace. How should I configure it?
user855
  • 19,048
  • 38
  • 98
  • 162
9
votes
1 answer

How to execute multiple async functions at once and get the results?

I have tried Tokio tasks, but there are no working examples to execute multiple tasks at once. What is wrong with this code? fn main() { block_on(speak()); } async fn speak() { let hold = vec![say(), greet()]; let results =…
Alexei Kitaev
  • 159
  • 1
  • 4
9
votes
2 answers

How do I schedule a repeating task in Tokio?

I am replacing synchronous socket code written in Rust with the asynchronous equivalent using Tokio. Tokio uses futures for asynchronous activity so tasks are chained together and queued onto an executor to be executed by a thread pool. The basic…
locka
  • 5,809
  • 3
  • 33
  • 38
8
votes
1 answer

What is the best way to convert an AsyncRead to a TryStream of bytes?

I have an AsyncRead and want to convert it to a Stream> with tokio 0.2 and futures 0.3. The best I've been able to do is something like: use bytes::Bytes; // 0.4.12 use futures::stream::{Stream, TryStreamExt};; //…
Thayne
  • 6,619
  • 2
  • 42
  • 67
8
votes
2 answers

understanding error: trait `futures::future::Future` is not implemented for `()`

This question is about how to read the Rust documentation and improve my understanding of Rust so as to understand how to address this specific compiler error. I've read the tokio docs and experimented with many of the examples. In writing my own…
Ultrasaurus
  • 3,031
  • 2
  • 33
  • 52
8
votes
2 answers

tokio-async-await with trait

I'd like to write async functions in a trait, but since async fn in traits are not supported yet, I am trying to find the equivalent method interface. This is what I have tried in Rust nightly (2019-01-01): playground #![feature(await_macro,…
mq7
  • 1,125
  • 2
  • 11
  • 21
8
votes
2 answers

How to set timeout for HTTP request with hyper, tokio and futures in Rust?

How do I set a timeout for HTTP request using asynchronous Hyper (>= 0.11)? Here is the example of the code without timeout: extern crate hyper; extern crate tokio_core; extern crate futures; use futures::Future; use hyper::Client; use…
Sergey Potapov
  • 3,819
  • 3
  • 27
  • 46
8
votes
2 answers

How to build multiple concurrent servers with Rust and Tokio?

I'm looking to build multiple concurrent servers on different ports with Rust and Tokio: let mut core = Core::new().unwrap(); let handle = core.handle(); // I want to bind to multiple port here if it's possible with simple addresses let addr =…
Thibaud Dauce
  • 367
  • 4
  • 14
8
votes
1 answer

Repeating a Rust task with tokio_timer

I'm creating a repeating task in Rust using the Tokio framework. The following code is based on a completed change request to add this feature to the tokio-timer crate. When trying to compile, I get the error message: error[E0281]: type mismatch:…
Mark Sivill
  • 825
  • 1
  • 9
  • 18
7
votes
1 answer

How to log and filter requests with Axum/Tokio?

I am using Axum for a relatively simple Web API and would like to get a logging/tracing output for incoming requests similar to Go Gin, IIS logs, Python FastAPI, etc. - a simple path and parameters output. The layer for HTTP is added to the…
Alen Siljak
  • 2,482
  • 2
  • 24
  • 29
7
votes
2 answers

Can tokio be understood as similar to Javascripts event loop or be used like it?

I'm not sure if tokio is similar to the event loop in Javascript, also a non-blocking runtime, or if it can be used to work in a similar way. In my understanding, tokio is an runtime for futures in Rust. Therefore it must implement some kind of…
phip1611
  • 5,460
  • 4
  • 30
  • 57
7
votes
1 answer

What is the smallest feature set to enable polling a future with Tokio?

I want to poll an async function: #[tokio::main] async fn main() -> Result<(), Box> { some_function().await; } I am currently activating all features: tokio = { version = "1.4.0", features = ["full"] } which of those are…
Gatonito
  • 1,662
  • 5
  • 26
  • 55
7
votes
1 answer

Future created by async block is not `Send` because of *mut u8

I was able to proceed forward to implement my asynchronous udp server. However I have this error showing up twice because my variable data has type *mut u8 which is not Send: error: future cannot be sent between threads safely help: within `impl…
Antonin GAVREL
  • 9,682
  • 8
  • 54
  • 81
1 2
3
70 71