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

Tokio error: "there is no reactor running" even with #[tokio::main] and a single version of tokio installed

when running code like this: use futures::executor; ... pub fn store_temporary_password(email: &str, password: &str) -> Result<(), Box> { let client = DynamoDbClient::new(Region::ApSoutheast2); ... let future =…
Chris
  • 39,719
  • 45
  • 189
  • 235
7
votes
1 answer

Can I miss a value by calling select on two async receivers?

Is it possible, if a task sends to a and an other (at the same time) sends to b, that tokio::select! on a and b drops one of the value by cancelling the remaining future? Or is it guaranteed to be received at the next loop iteration? use…
uben
  • 1,221
  • 2
  • 11
  • 20
7
votes
1 answer

Why implementation of iterator is not generic enough in async context?

Cross posting on github Given the following snippet: use futures::stream::{self, StreamExt}; async fn from_bar(bar: &[Vec<&u8>]) { let x = bar.iter().flat_map(|i| i.iter().map(|_| async { 42 })); let foo: Vec<_> =…
Stargateur
  • 24,473
  • 8
  • 65
  • 91
7
votes
1 answer

How to write a hyper response body to a file?

I am trying to write a test program with tokio that grabs a file from a website and writes the streamed response to a file. The hyper website shows an example that uses a while loop and uses the .data() method the response body, but I'd like to…
broughjt
  • 73
  • 4
7
votes
1 answer

How do I solve the error "thread 'main' panicked at 'no current reactor'"?

I am trying to connect to a database: extern crate tokio; // 0.2.6, features = ["full"] extern crate tokio_postgres; // 0.5.1 use futures::executor; use tokio_postgres::NoTls; fn main() { let fut = async { let (client, connection) =…
julia
  • 139
  • 3
  • 11
7
votes
1 answer

Is there any way to create a async stream generator that yields the result of repeatedly calling a function?

I want to build a program that collects weather updates and represents them as a stream. I want to call get_weather() in an infinite loop, with 60 seconds delay between finish and start. A simplified version would look like this: async fn…
peku33
  • 3,628
  • 3
  • 26
  • 44
7
votes
2 answers

How can I use the question mark operator to handle errors in Tokio futures?

I have a client handling a Future that does some stuff. Is it possible to use impl Future as a return type and make better error handling? pub fn handle_client(client: Client) -> impl Future
abritov
  • 373
  • 4
  • 11
7
votes
1 answer

How do I use async/await syntax with Tokio?

I'm trying to use async/await with processes in Rust. I'm using tokio and tokio-process: #![feature(await_macro, async_await, futures_api)] extern crate tokio; extern crate tokio_process; use std::process::Command; use…
Hristo Kolev
  • 1,486
  • 1
  • 16
  • 33
7
votes
1 answer

Shared mutable state in Hyper

I'm trying to create a counter in a Hyper web server that counts the number of requests it has received. I'm using a Arc> to hold onto count. However, I haven't been able to figure out the right combination of move and .clone() to satisfy…
Michael Snoyman
  • 31,100
  • 3
  • 48
  • 77
7
votes
3 answers

How can I test a future that is bound to a tokio TcpStream?

I have a future which wraps a TCP stream in a Framed using the LinesCodec. When I try to wrap this in a test, I get the future blocking around 20% of the time, but because I have nothing listening on the socket I'm trying to connect to, I expect to…
Martin
  • 920
  • 7
  • 24
7
votes
1 answer

Running asynchronous mutable operations with Rust futures

I'm building a service in Rust using tokio-rs and was happy with this tech stack so far. I'm now trying to chain up async operations that includes writes and having a hard time with the borrow checker. My simplified minimal code sample is…
Zólyomi István
  • 2,401
  • 17
  • 28
7
votes
2 answers

How to select between a future and stream in Rust?

I've just started experimenting with futures/tokio in Rust. I can do really basic things with just futures or just with streams. I was wondering how you can select between future and a stream. How can I extend the toy problem from the tokio…
opensourcegeek
  • 5,552
  • 7
  • 43
  • 64
6
votes
1 answer

the trait `std::marker::Sync` is not implemented for `std::sync::mpsc::Sender

I'm attempting to build a multithreaded application using MPSC and I'm running into the error in the title. I'm not sure what the proper pattern for this use case is - I'm looking for a pattern that will allow me to clone the producer channel and…
David Miner
  • 198
  • 2
  • 10
6
votes
2 answers

"future cannot be sent between threads safely" when pass Arc into tokio::spawn

I implemented TCP client using tokio. However, my code not compile because I got an error: error: future cannot be sent between threads safely --> src/main.rs:81:9 | 81 | tokio::spawn(async move { | ^^^^^^^^^^^^ future…
Sergio Ivanuzzo
  • 1,820
  • 4
  • 29
  • 59
6
votes
2 answers

Rust cannot link with `x86_64-w64-mingw32-gcc`

I am trying to make a get request to a url with rust and everytime I run this project I get this error, my other rust project work fine. here's my cargo.toml file. [package] name = "api_req" version = "0.1.0" edition = "2021" # See more keys and…
SomeOnionGamer
  • 203
  • 1
  • 4
  • 8