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

How to send data from a tokio TcpStream using actix-web HttpResponse

I have a TcpStream from which I need to read N bytes and send them as HttpResponse (I will still need to use the stream later on). At the moment I'm using the following code: use futures::channel::mpsc; use actix_web::HttpResponse; use…
Nick
  • 10,309
  • 21
  • 97
  • 201
3
votes
2 answers

Why does `tokio::main` report the error "cycle detected when processing"?

I'm using Tokio and async/.await to create an UDP server where I can receive and send data in an asynchronous way. The SendHalf of my UDP socket is shared across more than one task. To do this I’m using Arc>. That's why…
createproblem
  • 1,542
  • 16
  • 30
3
votes
1 answer

How to rejoin `ReadHalf` and `WriteHalf` into a `TcpStream`

Is there a way to rejoin a Tokio ReadHalf and WriteHalf into a TcpStream after a split? let mut stream = TcpStream::connect("127.0.0.1:8080").await?; let (reader, writer) = stream.split(); // re-join pseudo-code let stream: TcpStream = (reader,…
Nick
  • 10,309
  • 21
  • 97
  • 201
3
votes
1 answer

Manually polling streams in future implementation

I'm in the process of migrating to futures 0.3 and tokio 0.2, and there is one recurring pattern I can't manage to re-use. I'm not sure whether this pattern became obsolete or whether I'm doing something wrong wrt to Pin. Usually I have one type…
little-dude
  • 1,544
  • 2
  • 17
  • 33
3
votes
1 answer

How do I spawn many cancellable timers using Tokio?

How do I use Tokio to implement a fixed number of timers that are regularly reset and canceled across threads? When a timer expires, a callback will be executed. An API similar to that of Go's time.AfterFunc is essentially what I desire: package…
Cshark
  • 101
  • 6
3
votes
2 answers

Example usage of hyper with bb8 and postgres

I want to use hyper with bb8 and tokio-postgres. In every request I want to acquire a new connection from the pool. Can anybody provide me some example for this scenario? Currently I do it like this: fn main() { let addr =…
sivakov512
  • 415
  • 5
  • 14
3
votes
2 answers

How can I run a set of functions concurrently on a recurring interval without running the same function at the same time using Tokio?

My goal is to run N functions concurrently but don't want to spawn more until all of them have finished. This is what I have so far: extern crate tokio; extern crate futures; use futures::future::lazy; use std::{thread, time}; use…
nbari
  • 25,603
  • 10
  • 76
  • 131
3
votes
1 answer

buffer in rust-tokio streams is there a way to use something other then &[u8]?

I am trying to make a echo server that capitalize a String when it replies, to practice with tokio as an exercise. I used an array as a buffer which is annoying because what if the string overflows the buffer? I would like to know if there is a…
lucarlig
  • 141
  • 3
  • 12
3
votes
2 answers

How do I asynchronously calculate the checksum of a file on the hard disk in Rust?

I have a TCP file server in Rust / Tokio stack. When a client is uploading a file, the data is being read from a tokio::net::TcpStream and written to a futures_fs::FsWriteSink, which has been started on a separate futures_fs::FsPool. When the file…
hedgar2017
  • 1,425
  • 3
  • 21
  • 40
3
votes
1 answer

How do I read_until the tokio::net::TcpStream in a future chain?

I would like to read data from the TcpStream until I encounter a '\0'. The issue is that tokio::io::read_until needs the stream to be BufRead. fn poll(&mut self) -> Poll<(), Self::Error> { match self.listener.poll_accept()? { …
hedgar2017
  • 1,425
  • 3
  • 21
  • 40
3
votes
1 answer

Writing a chunk stream to a file asynchronously using hyper

I am trying to create a simple function that downloads a remote file to a local filepath using hyper. I need the file write to be asynchronous as well (in my case I am using tokio_fs for that). Here is the code: View in the playground // Parts of…
lhahn
  • 1,241
  • 2
  • 14
  • 40
3
votes
1 answer

Run async function in specific thread

I would like to run specific long-running functions (which execute database queries) on a separate thread. However, let's assume that the underlying database engine only allows one connection at a time and the connection struct isn't Sync (I think…
mrspl
  • 481
  • 1
  • 5
  • 10
3
votes
1 answer

Why does Tokio's Runtime::block_on_all require a future with a 'static lifetime?

When dealing with Tokio and futures, the futures passed to the Tokio runtime must have a 'static lifetime most of the time. I understand that because the future might be moved to another thread where it can potentially outlive the place it was…
Mildred
  • 463
  • 4
  • 13
3
votes
1 answer

How to use tokio's UdpSocket to handle messages in a 1 server: N clients setup?

What I want to do: ... write a (1) server/ (N) clients (network-game-)architecture that uses UDP sockets as underlying base for communication. Messages are sent as Vec, encoded via bincode (crate) I also want to be able to occasionally send…
user4063815
3
votes
1 answer

Getting multiple URLs concurrently with Hyper

I am trying to adapt the Hyper basic client example to get multiple URLs concurrently. This is the code I currently have: extern crate futures; extern crate hyper; extern crate tokio_core; use std::io::{self, Write}; use std::iter; use…
user964375
  • 2,201
  • 3
  • 26
  • 27