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

the trait bound `tokio::net::tcp::stream::TcpStream: tokio_io::async_read::AsyncRead` is not satisfied

I can't compile a simple application to test tokio-codec. tokio::net::tcp::stream::TcpStream implements AsyncRead and -Write. But when i try to compile the code below, i get the error below. I am still new to Rust and Tokio, so no doubt im missing…
5
votes
2 answers

How to asynchronously explore a directory and its sub-directories?

I need to explore a directory and all its sub-directories. I can explore the directory easily with recursion in a synchronous way: use failure::Error; use std::fs; use std::path::Path; fn main() -> Result<(), Error> { …
Nick
  • 10,309
  • 21
  • 97
  • 201
5
votes
2 answers

HTTP request inside actix-web handler -> Multiple executors at once: EnterError

When creating a hyper post request inside an actix-web resolver, the following error is thrown - how can one send one a http request by spawning the request into the existing executor? thread 'actix-rt:worker:1' panicked at 'Multiple executors at…
Techradar
  • 3,506
  • 3
  • 18
  • 28
5
votes
1 answer

Multicast UDP packets using Tokio futures

I'm playing around with Tokio and Rust and as an example, I am trying to write a simple UDP proxy that will just accept UDP packets on one socket and send it out to multiple other destinations. However, I stumble over the situation that I need to…
Mats Kindahl
  • 1,863
  • 14
  • 25
5
votes
1 answer

How do I stream a hyper Request's Body from a slow-processing side thread that produces chunks of data?

I have a program which generates data slowly (we can say it's computationally intensive, like computing digits of pi). It produces a lot of data; each response can be 1GiB, will not fit in memory, and must be generated on demand. I'm using hyper to…
njaard
  • 529
  • 4
  • 15
5
votes
0 answers

How can i make a piece of running code timeout?

I'm trying to get some piece of code to run for at most timeout duration. In other words I want a piece of code to either complete in the given time constraint or be interrupted. The concrete code that would be running would be a multi-part http…
vuk
  • 91
  • 8
5
votes
1 answer

Why does calling tokio::spawn result in the panic "SpawnError { is_shutdown: true }"?

I want to use Delay to do some work later. If I use tokio::run, it just works fine, but it panics when using tokio::spawn: use std::sync::mpsc; use std::time::*; use tokio::prelude::*; // 0.1.14 fn main() { let (tx, rx) = mpsc::channel(); …
hr567
  • 53
  • 1
  • 4
5
votes
1 answer

How can I implement a blocking queue mechanism with futures::sync::mpsc::channel?

I am trying to understand how futures::sync::mpsc::Receiver works. In the below example, the receiver thread sleeps for two seconds and the sender sends every second. I expect that the sender will need to be blocked because of the wait and then…
Akiner Alkan
  • 6,145
  • 3
  • 32
  • 68
5
votes
1 answer

How do I limit the Tokio threadpool to a certain number of native threads?

What's the correct way of limiting the Tokio (v 0.1.11) threadpool to n OS native threads, where n is an arbitrary number, preferably configurable at runtime? As far as I can tell, it's possible to use Tokio in single threaded mode using using…
George
  • 3,521
  • 4
  • 30
  • 75
5
votes
1 answer

How can I send a stream of data using Tokio's TcpStream?

I am trying to implement a TCP client in Rust. I am able to read data coming from the server but I can not manage to send data. Here is the code that I am working on: extern crate bytes; extern crate futures; extern crate tokio_core; extern crate…
Akiner Alkan
  • 6,145
  • 3
  • 32
  • 68
5
votes
0 answers

What does the 'static lifetime mean in a trait bound in a Rust future?

I thought that I've got what the 'static lifetime is, but now I'm not sure. I'm trying to understand how to work with Tokio and Futures. My app is working, but the structure is awful, so I need to decompose it. Here comes 'static requirement for my…
fevgenym
  • 568
  • 7
  • 20
5
votes
0 answers

How do I append futures to a BufferUnordered stream?

I'm trying to append futures to the underlying stream of BufferUnordered. At the moment I'm pushing them directly into the underlying stream, the Fuse stream of BufferUnordered is empty, so pushing to it has no effect and the loop below does not…
twissel
  • 51
  • 2
5
votes
1 answer

Sharing mutable state between clients using async (tokio) rust-websocket

I am writing a websocket server in Rust using rust-websocket and its Tokio-based async system. I can serve clients just fine, however, I can not figure out how to share mutable state between the clients. Here is some (partial) code demonstrating…
rytone
  • 155
  • 2
  • 11
5
votes
1 answer

How can I use hyper::client from another thread?

I have multiple threads performing some heavy operations and I need to use a client in middle of work. I'm using Hyper v0.11 as a HTTP client and I would like to reuse the connections so I need to share the same hyper::Client in order to keep open…
cspinetta
  • 443
  • 6
  • 17
4
votes
1 answer

How to correctly read a string value from an outer scope within an async closure for Hyper in Rust

I'm trying to learn Rust, and trying to write some extremely simple web server code to do so. I thought I had a good idea of the basics of lifetimes & borrowing in simple code, but I'm finding that either I'm missing a basic technique somewhere, or…
Tim Perry
  • 11,766
  • 1
  • 57
  • 85