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
6
votes
2 answers

How to pass a Future as a function argument?

I am used to Scala's Future type where you wrap whatever object you're returning in Future[..] to designate it as such. My Rust function hello returns Query and I don't seem able to pass that result as an argument with type Future.…
ecoe
  • 4,994
  • 7
  • 54
  • 72
6
votes
1 answer

The proper method to get tokio runtime handle based on current running environment

What's the idiomatic way to get a tokio runtime handle based on the current running environment? For methods already run in tokio runtime, I want to use Handle.try_current().unwrap() to get the current one. For methods not run in tokio, I can…
yjshen
  • 6,583
  • 3
  • 31
  • 40
6
votes
1 answer

Efficient synchronization primitives for a single-threaded async app in Rust

I have a tokio-based single-threaded async app where using Arcs or other Sync types seems to be an overhead. Because there is no need for synchronization between threads, I am looking for something like tokio::sync::oneshot::channel, Sender and…
Fedorov7890
  • 1,173
  • 13
  • 28
6
votes
3 answers

Spawn non-static future with Tokio

I have an async method that should execute some futures in parallel, and only return after all futures finished. However, it is passed some data by reference that does not live as long as 'static (it will be dropped at some point in the main…
msrd0
  • 7,816
  • 9
  • 47
  • 82
6
votes
3 answers

Rust doc test with async function #[tokio-test]

It seems one cannot use #[tokio-test] for test async functions in the Rust doc test? For now I have to write an async main function and tag it with #[tokio-main] and call test_fn().await in it to let some async function run during cargo test…
Fuyang Liu
  • 1,496
  • 13
  • 26
6
votes
1 answer

Why do I get a deadlock when using Tokio with a std::sync::Mutex?

I stumbled upon a deadlock condition when using Tokio: use tokio::time::{delay_for, Duration}; use std::sync::Mutex; #[tokio::main] async fn main() { let mtx = Mutex::new(0); tokio::join!(work(&mtx), work(&mtx)); println!("{}",…
Konstantin W
  • 409
  • 3
  • 11
6
votes
1 answer

(tokio::spawn) borrowed value does not live long enough -- argument requires that `sleepy` is borrowed for `'static`

This MWE shows the use of tokio::spawn in for in loop. The commented code sleepy_futures.push(sleepy.sleep_n(2)); works fine, but does not run/poll the async function. Basically, I would like to run a bunch of async functions at the same time. I…
chmoder
  • 876
  • 10
  • 19
6
votes
1 answer

Is there a way to use tokio::main with a single threaded runtime in tokio 0.2?

Is there a way to specify a single-threaded runtime using the attribute #[tokio::main] in tokio 0.2? The doc does not seem to have examples for that. EDIT: I wanted to find a way to set up tokio runtime so that rustc is aware of tokio:spawn() will…
user1783732
  • 1,599
  • 5
  • 22
  • 44
6
votes
1 answer

Is it possible to check if a future is ready, without consuming its output?

Futures in Rust can be polled without blocking to check if the future is ready (and do some work in the process). With that in mind, is it possible to "check" if a future is ready, without consuming its output? Possible scenario stdin would be…
mr.celo
  • 585
  • 1
  • 5
  • 17
6
votes
1 answer

How do I add tasks to a Tokio event loop that is running on another thread?

I'd like to spin up a Tokio event loop alongside a Rocket server, then add events to this loop later on. I read Is there a way to launch a tokio::Delay on a new thread to allow the main loop to continue?, but it's still not clear to me how to…
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
6
votes
1 answer

Spawning tasks with non-static lifetimes with tokio 0.1.x

I have a tokio core whose main task is running a websocket (client). When I receive some messages from the server, I want to execute a new task that will update some data. Below is a minimal failing example: use tokio_core::reactor::{Core,…
Ben Ruijl
  • 4,973
  • 3
  • 31
  • 44
6
votes
3 answers

How to cancel an infinite stream from within the stream itself?

I'm trying to cancel an interval (interval_timer) after emptying a queue but not sure what is the right strategy. let mut some_vars = vec![1, 2, 3, 4, 5, 6, 7, 8]; let interval_timer = tokio_timer::Timer::default(); let timer = interval_timer …
opensourcegeek
  • 5,552
  • 7
  • 43
  • 64
5
votes
1 answer

Copy reqwest bytes_stream in to tokio file

I'm attempting to copy a file downloaded with reqwest in to a tokio file. This file is too large to store in memory so it needs to be through the bytes_stream() rather than bytes() I had attempted the following let mut tmp_file =…
Qwertie
  • 5,784
  • 12
  • 45
  • 89
5
votes
3 answers

How to gracefully shutdown a warp server?

I'm writing a service with warp in Rust. When the service receives a SIGTERM signal, I'd like to have it shutdown gracefully and possibly do some logging or other work. I have tried a number of examples and nothing works. The most promising seems to…
DungeonTiger
  • 627
  • 1
  • 9
  • 21
5
votes
2 answers

How to deal with tokio::spawn closure required to be 'static and &self?

I'm having trouble understanding how to write concurrent async code encapsulated in one single structure. I'm not sure how to explain the problem exactly, so i'll try to do it with an example. Let's say I have a UdpServer struct. This struct has…
Bonanov
  • 55
  • 1
  • 5