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

How can I stop reading from a tokio::io::lines stream?

I want to terminate reading from a tokio::io::lines stream. I merged it with a oneshot future and terminated it, but tokio::run was still working. use futures::{sync::oneshot, *}; // 0.1.27 use std::{io::BufReader, time::Duration}; use…
nkkr
  • 101
  • 5
4
votes
1 answer

How does tokio::net::TcpStream implement tokio::prelude::Stream?

In the tokio.rs docs we see the following snippet // split the socket stream into readable and writable parts let (reader, writer) = socket.split(); // copy bytes from the reader into the writer let amount = io::copy(reader, writer); I am assuming…
benjumanji
  • 1,395
  • 7
  • 15
4
votes
2 answers

What's a clean way to get how long a future takes to resolve?

I'm working with Tokio doing some UDP stuff. I want to record the amount of time my UDP probe future takes to resolve. I came up with the following function, time_future(), to wrap a future and give me the result and a duration. The function seems…
4
votes
2 answers

How do I use Tokio Reactor in a #[no_std] environment?

I am trying to implement futures on the Tock OS embedded operating system. I'm trying to using Tokio in a #[no_std] environment. My Cargo.toml file looks like this: [package] name = "nrf52dk" version = "0.1.0" authors = ["Tock Project Developers…
EmbeddedOS
  • 173
  • 6
  • 18
4
votes
2 answers

How to read subprocess output asynchronously

I want to implement a futures::Stream for reading and parsing the standard output of a child subprocess. What I'm doing at the moment: spawn subprocess and obtain its stdout via std::process methods: let child =…
4
votes
2 answers

Deserialize from tokio socket

I am using tokio to implement a server which communicates with messages serialized with serde (bincode). Without asynchronous and futures I would do extern crate tokio_io; extern crate bincode; extern crate serde; extern crate bytes; extern crate…
Manuel Schmidt
  • 2,429
  • 1
  • 19
  • 32
4
votes
1 answer

How to display a welcome message / banner when a client connects to a tokio-proto server?

A SMTP server should display a welcome message upon establishing connection (220 service ready) which is a signal for the client to start sending commands. This seems to be in conflict with the request-response paradigm of tokio-proto. I can imagine…
Robert Cutajar
  • 3,181
  • 1
  • 30
  • 42
3
votes
2 answers

How to transfer latest state between threads in rust without using a mutex

I am working on a Rust project where I have two distinct threads that serve specific purposes. One is a real-time thread which interfaces directly with a piece of hardware and maintains strict timing requirements, while the other is a web server…
WinKey
  • 41
  • 5
3
votes
1 answer

tokio::spawn with and without async block

I am learning async/await in Rust, and I was following tokio tutorial. In the tutorial, there is one thing that is bothering me, but the tutorial does not seem to explain. Consider this tokio::spawn(async move { process(socket).await; }); This…
techhara
  • 77
  • 5
3
votes
2 answers

Using `&mut self` in an async move block

I have a WriterJob struct. Its functionality is not important; what it does is that it spawns a long-running task and provides callers an api to check the status of the job. pub(crate) struct WriterJob { ..., status: Status, } impl…
Kiwi breeder
  • 459
  • 4
  • 11
3
votes
1 answer

Having problem in Rust with Tokio and daemonize. How to get them to work together?

I have written a simple program that uses Tokio and fern to accept input from TCP and log it to the stdio and to a file. Now, I wanted to have the program run on my remote server in the background so it can log at any time whether I am connected by…
Jordan
  • 711
  • 1
  • 6
  • 12
3
votes
1 answer

Bind two UDP sockets to the same address and connect them to different addresses

I need to create a distributed system comprised of various processes; to simplify matters a bit, let's say they are three: A, B and C. Every process knows the IP and port of the others from/to which to receive/send UDP datagrams. They all use the…
steddy
  • 146
  • 1
  • 9
3
votes
1 answer

What's the difference between tokio::select! A, B inside a loop and tokio::spawn two tasks each runs one of A, B?

I'm new to asynchronous programming, therefore struggling with how different approach varies in behavior. Consider the example given by tokio at github repo, chat.rs: // snip loop { tokio::select! { // A message was received…
3
votes
1 answer

How to store a list of closures returning a Future and share it between threads in Rust?

I'm trying to write a multi-thread TCP server that can handle multiple connections at the same time with tokio. I want to structure it in an event-driven way, where it's possible to attach one or more closures to a specific event (like new…
Lukasz Kujawa
  • 3,026
  • 1
  • 28
  • 43
3
votes
2 answers

How to find number of active tokio task?

I would like to get the count of active running tokio tasks. In python, I can use len(asyncio.all_tasks()) which returns the unfinished tasks for the current running loop. I would like to know any equivalent in tokio. Here is a sample code: use…
coder3101
  • 3,920
  • 3
  • 24
  • 28