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 do I convert a dashmap to hashmap

I recently decided to use dashmap since tokio recommends it instead of implementing sharding myself. In some cases, where I only need readable access to it, I would like to convert the dashmap to hashmap so I can do minimal changes in those…
Mahesh Bansod
  • 1,493
  • 2
  • 24
  • 42
3
votes
1 answer

Why can't tokio listen for signals between SIGRTMIN and SIGRTMAX?

I'm trying to listen to signals between libc::SIGRTMIN() and libc::SIGRTMAX() with tokio, but it fails with "signal too large". However, I was able to listen to these signals with the signal-hook crate. I tracked the error down with gdb, and the…
weisbrja
  • 164
  • 10
3
votes
1 answer

How do I simultaneously read messages from multiple Tokio channels in a single task?

I'd like to both read and process messages from two channels and construct another message and send this message via another channel. Messages from the two channels are received at different frequencies (as per sleep). Example: "foo1" and "bar1" are…
user270199
  • 905
  • 1
  • 6
  • 12
3
votes
1 answer

How do I run an asynchronous task periodically and also sometimes on demand?

I have a task (downloading something from the Web) that runs regularly with pauses 10 min between runs. If my program notices that the data is outdated, then it should run the download task immediately unless it is already running. If the download…
porton
  • 5,214
  • 11
  • 47
  • 95
3
votes
0 answers

Tokio spawn_blocking when passing reference requires a static lifetime

I am quite new to Rust and have some problems with async and lifetimes. I am trying to read a USB cam in a loop using rscam crate. The problem is that after some time the camera driver tends to freeze and returns no data. Rscam's capture has no…
3
votes
1 answer

Limiting the number of concurrent futures in join_all!()

How to make Rust execute all given futures (like join_all!) limiting to execute say 10 futures at once? I need to download files from a big number of servers, but query no more than 10 servers simultaneously (to accurately measure their timeouts: if…
porton
  • 5,214
  • 11
  • 47
  • 95
3
votes
0 answers

How to solve "implementation of `sqlx::Acquire` is not general enough"

I'm creating a actix_web app. I tried to spawn a new task and use functions, which are already in use in actix_web part. But if I compile, I see following error with some life-time information. To be honest, I've no idea what to do with this…
macalloy
  • 43
  • 7
3
votes
3 answers

How to conditionally run one of two functions with tokio::join?

I need to run 4 functions concurrently but one of them is different based on user input. If I use "if-else" I get "if and else have incompatible types" due to Future. The only way I see is to make a third function that selects from the other two,…
PFA hard
  • 81
  • 5
3
votes
1 answer

A collection of interconnected futures in async Rust w/ tokio

Problem statement I'd like to implement a directed acyclic computation graph framework in async Rust, i.e. an interconnected graph of computation "nodes", each of which takes inputs from predecessor nodes and produces outputs for successor nodes. I…
Josh Burkart
  • 438
  • 4
  • 12
3
votes
2 answers

Tokio non blocking background task leads to error `self` has an anonymous lifetime `'_` but it needs to satisfy a `'static` lifetime requirement

I have an async function which I want to run in the background. This function is part of a call hierarchy which is not using async calls. My call hierarchy looks like this: struct Handler {} impl Handler { pub async fn handle(&self) { ///…
Avba
  • 14,822
  • 20
  • 92
  • 192
3
votes
1 answer

how to monitor reqwest client upload progress

for downloading with reqwest and tokio and progress I am using the code below pub async fn download_file(client: &Client, url: &str, path: &str) -> Result<(), String> { // Reqwest setup let res = client .get(url) .send() …
ozkanpakdil
  • 3,199
  • 31
  • 48
3
votes
0 answers

Messaging between two tokio runtimes inside separate threads

I ran into the kind of a problem described in this question: How can I create a Tokio runtime inside another Tokio runtime without getting the error "Cannot start a runtime from within a runtime"? . Some good rust crates doesn't have asynchronous…
Lex
  • 194
  • 1
  • 2
  • 11
3
votes
2 answers

Can I clone a future?

I want to write some generic retry logic for a future. I know the concrete return type and want to retry the same future. My code only has access to the future - I do not want to wrap every fn call site in a closure to enable recreating it. It seems…
zino
  • 1,222
  • 2
  • 17
  • 47
3
votes
2 answers

How can I `fut.await` to run a future in the "background" (execute it but not wait for it)?

I want to able to start a future running in the background, and not wait for it immediately in the parent function scope. Something like a dynamic join_all where I can add new futures in a loop to a set, and then pass the set to another function…
zino
  • 1,222
  • 2
  • 17
  • 47
3
votes
1 answer

Is there a way to create multiple pools for tokio::spawn_blocking so some tasks don't starve others?

I am using Tokio for some asynchronous Rust code, and have run into a problem. I have some tasks which require access to a connection pool, and the nature of the connection pool means that only a fixed number (NUMCPUS) can run at a time - all other…
Antimony
  • 37,781
  • 10
  • 100
  • 107