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

How can I monitor stalled tasks?

I am running a Rust app with Tokio in prod. In the last version i had a bug, and some requests caused my code to go into an infinite loop. What happened is while the task that got into the loop was stuck, all the other task continue to work well and…
Eyal leshem
  • 995
  • 2
  • 10
  • 21
4
votes
1 answer

How to translate JS Promises to Rust

at the moment I'm writing a pure Rust MQTT5 library (I know there are existing ones out there, but I'm more trying to learn Rust) and I stumpled upon this problem. I'm using latest stable rust with tokio 1.0.1. When I send out a packet over the…
Snapstromegon
  • 330
  • 3
  • 8
4
votes
1 answer

Does tokio::time::sleep method take task off the run queue?

I was looking to tokio source code to get the answer for the question and I was under the impression that the sleep method literally puts a timer with duration but I think i might have misunderstood the code because doing so would be highly…
pandawithcat
  • 571
  • 2
  • 13
4
votes
1 answer

Why does tokio::spawn have a delay when called next to crossbeam_channel::select?

I'm creating a task which will spawn other tasks. Some of them will take some time, so they cannot be awaited, but they can run in parallel: src/main.rs use crossbeam::crossbeam_channel::{bounded, select}; #[tokio::main] async fn main() { let…
Krzysiu
  • 43
  • 4
4
votes
1 answer

How to enumerate over columns with tokio-postgres when the field types are unknown at compile-time?

I would like a generic function that converts the result of a SQL query to JSON. I would like to build a JSON string manually (or use an external library). For that to happen, I need to be able to enumerate the columns in a row dynamically. let rows…
Kind Contributor
  • 17,547
  • 6
  • 53
  • 70
4
votes
1 answer

Wrapping an AsyncRead

I can't seem to get the compiler to let me wrap a Tokio AsyncRead: use std::io::Result; use core::pin::Pin; use core::task::{Context, Poll}; use tokio::io::AsyncRead; struct Wrapper{ inner: T } impl AsyncRead for…
robmor
  • 71
  • 6
4
votes
1 answer

What's a good detailed explanation of Tokio's simple TCP echo server example (on GitHub and API reference)?

Tokio has the same example of a simple TCP echo server on its: GitHub main page (https://github.com/tokio-rs/tokio) API reference main page (https://docs.rs/tokio/0.2.18/tokio/) However, in both pages, there is no explanation of what's actually…
4
votes
2 answers

How to create a Stream from reading and transforming a file?

I'm trying to read a file, decrypt it, and return the data. Because the file is potentially very big, I want to do this in a stream. I cannot find a good pattern to implement the stream. I'm trying to do something like this: let stream =…
user1783732
  • 1,599
  • 5
  • 22
  • 44
4
votes
1 answer

Unable to use self signed certificates with tokio-rustls

I'm trying to get the examples under https://github.com/quininer/tokio-rustls/tree/master/examples working. I am using self signed key/cert, generated using the openssl tool. But the handshake fails with a webpki error: Error: Custom { kind:…
rusty
  • 929
  • 1
  • 5
  • 10
4
votes
0 answers

What are the gotchas of using Tokio's spawn_blocking?

Is there anything I need to watch out for when using tokio::task::spawn_blocking? I couldn't find much on the thread pool used to run these tasks. Is this thread pool pre-created? Can it grow unbounded in size?
rusty
  • 929
  • 1
  • 5
  • 10
4
votes
2 answers

Panic running async code while dropping Rust Future

I have an app using #[tokio::main], that creates several resources on AWS in one of its tasks. I've implemented cleaning up those resources in Drop handlers. However those cleanup functions are async, so I am using block_on to ensure they run…
Matt Joiner
  • 112,946
  • 110
  • 377
  • 526
4
votes
1 answer

How to bind() on TCP client side in rust/tokio?

I have a need to make sure the client side of the TCP connection goes through a particular (IP) interface. The standard way would be to bind() the socket to the IP:0, before the connect(). I started looking at tokio::net::TcpStream::connect() and…
rusty
  • 929
  • 1
  • 5
  • 10
4
votes
1 answer

The trait `std::future::Future` is not implemented for `std::result::Result`

I'm trying to run basic reqwest example: extern crate reqwest; extern crate tokio; #[tokio::main] async fn main() -> Result<(), reqwest::Error> { let res = reqwest::Client::new() .get("https://hyper.rs") .send() …
semanser
  • 2,310
  • 2
  • 15
  • 33
4
votes
1 answer

How do I use tokio::timer::Timeout with Future::wait?

I am trying to introduce a timeout in my RPC requests using tokio:timer:Timeout: use std::time::{Duration, Instant}; use tokio::prelude::*; use tokio::timer::Delay; fn main() { let when = Instant::now() + Duration::from_millis(4000); let…
pd176
  • 821
  • 3
  • 10
  • 20
4
votes
1 answer

How to copy file with tokio::fs

I am trying to copy a file using tokio for async operations. I saw that tokio doesn't expose any method like tokio::fs::copy which would do the job for me (like the equivalent std::fs::copy for sync operations). While trying to implement such a…
Nick
  • 10,309
  • 21
  • 97
  • 201