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

How does Tokio's Handle::block_on differ from Runtime::block_on?

How does tokio::runtime::Handle.block_on differ to tokio::runtime::Runtime.block_on? The Handle.block_on causes some code to hang whereas the Runtime.block_on works fine. This is how I create the Handle. The Runtime is the same minus the last 2…
cactus
  • 357
  • 4
  • 16
3
votes
1 answer

How can I send a message from a standard library spawned thread to a Tokio async task?

I have a setup where my program spawns several threads for CPU-bound computation using the std::thread::spawn. I need a GRPC server to handle incoming commands and also stream outputs done by the worker threads. I'm using tonic for the GRPC server,…
l3utterfly
  • 2,106
  • 4
  • 32
  • 58
3
votes
4 answers

How to reuse Tokio runtime in Rust FFI library

I want to write a FFI wrapper for sn_api library, which contains async functions. It will be used in single-threaded non-async code written in Red. I found, that the easy way is to use Runtime::new().unwrap().block_on(...) in every exported…
Maciej Łoziński
  • 812
  • 1
  • 10
  • 16
3
votes
1 answer

Read Childstdout without blocking

I'm trying to reproduce Shepmasters answer to this question, but getting the following compilation error. error[E0599]: the method `for_each` exists for struct `tokio::io::Lines>`, but its trait…
eletvon
  • 35
  • 3
3
votes
1 answer

Async loop on a new thread in rust: the trait `std::future::Future` is not implemented for `()`

I know this question has been asked many times, but I still can't figure out what to do (more below). I'm trying to spawn a new thread using std::thread::spawn and then run an async loop inside of it. The async function I want to run: #[tokio::main]…
ilmoi
  • 1,994
  • 2
  • 21
  • 45
3
votes
2 answers

How to generate random numbers in async rust?

The rng crate is neither Sync nor Send, so the rng::thread_rng can not cross the .await point. What is the simplest and beautiful way to generate random numbers in async rust? Generating A lot of numbers beforehead and then use them is ugly.
Myrfy
  • 575
  • 4
  • 11
3
votes
1 answer

How can I stop an async program from terminating when any task is running?

Let's say I have something like this, async fn do_update() { // here we store it. let task = task::spawn(async { let duration = Duration::from_millis(10); let mut stream = tokio::time::interval(duration); …
Evan Carroll
  • 78,363
  • 46
  • 261
  • 468
3
votes
2 answers

Retrieve sibling elements using the scraper crate

While learning Rust I am trying to build a simple web scraper. My aim is to scrape https://news.ycombinator.com/ and get the title, hyperlink, votes and username. I am using the external libraries reqwest and scraper for this and wrote a program…
Eka
  • 14,170
  • 38
  • 128
  • 212
3
votes
1 answer

How can future.then() return a Future?

The function below, taken from here: fn connection_for( &self, pool_key: PoolKey, ) -> impl Future>, ClientError>> { let checkout = self.pool.checkout(pool_key.clone()); let connect =…
Gatonito
  • 1,662
  • 5
  • 26
  • 55
3
votes
1 answer

How to show a total counter bar using indicatif with async?

I try to use the indicatif crate to show several progress bars of sub-tasks together with one progress bar counting all finished tasks. Here is my code: # Cargo.toml [dependencies] indicatif = "0.15.0" tokio = { version = "1", features = ["full"]…
Steve Z
  • 300
  • 2
  • 10
3
votes
1 answer

Actix Web Consuming %5 Cpu While Idling

I am trying to learn rust atm hence I've created simple todo web app with actix and mongodb and deployed to linux server (ubuntu 18.04) via docker. But I realized, even while no connection/request (ie. right after container start), cpu usage stays…
Blithe
  • 105
  • 1
  • 9
3
votes
1 answer

rust tokio: calling async function from sync closure

I have the following problem: I'm trying to call sync closure from async function, but sync closure has to later call another async function. I cannot make async closure, because they are unstable at the moment: error[E0658]: async closures are…
Blomex
  • 305
  • 2
  • 12
3
votes
1 answer

Get the first received value from an iterator of channels in rust

I have an iterator of futures::channel::mpsc::UnboundedReceiver. I want to handle every answer of the receiver, only one at a time, but while also handling other futures. This should be possible by looping over a futures::select!. But I need some…
Florian sp1rit
  • 575
  • 1
  • 7
  • 20
3
votes
1 answer

How to use future::join_all with the multiplexed redis in the async tokio runtime

I'm trying to use the Rust redis client in the asynchronous multiplexed mode, with tokio as the async runtime, and dynamic number of futures to join. I had success using future::join3 on a constant number of futures, but I want to multiplex many…
Gyfis
  • 1,174
  • 1
  • 14
  • 35
3
votes
1 answer

How do you get headers when using tokio-tungstenite?

I'm experimenting with the tokio-tungstenite crate to create chat rooms based on a URL. For example, I have a client connecting to ws://localhost:8080/abcd. My understanding is that I have to use the tokio_tungstenite::accept_hdr_async function to…
Henry
  • 195
  • 3
  • 15