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

Rust Axum Multipart Length Limit Exceeded

Referenced Axum documentation: docs.rs Hello all, I am trying to create a simple file upload using HTML5 forms and Rust Axum. The issue is that, while any normal file works, larger files (particularly video files) which I want to upload are too…
Ty Q.
  • 207
  • 2
  • 13
3
votes
2 answers

How to restructure struct such that 'Cannot borrow `x` as mutable more than once at a time' does not occur

Currently, I have one AppState struct that has two fields: pub struct AppState { players: HashMap, cars: HashMap, } I have implemented a multi-threaded server application using tokio that processes…
John Doe
  • 113
  • 1
  • 2
  • 11
3
votes
1 answer

How to cheaply send a delay message?

My requirement is very simple, which is a very reasonable requirement in many programs. It is to send a specified message to my Channel after a specified time. I've checked tokio for topics related to delay, interval or timeout, but none of them…
progquester
  • 1,228
  • 14
  • 23
3
votes
1 answer

How to terminate a blocking tokio task?

In my application I have a blocking task that synchronically reads messages from a queue and feeds them to a running task. All of this works fine, but the problem that I'm having is that the process does not terminate correctly, since the…
3
votes
1 answer

Why does !Send value that is drop()'d before .await mean the Future is !Send?

The logic of test_use_std_mutex_1 and test_use_std_mutex_2 is the same. Why does one report an error while the other compiles successfully? fn main() { let rt = tokio::runtime::Builder::new_multi_thread().worker_threads(2) …
3
votes
1 answer

Framing packets from a UDP stream in Tokio

Consider this small server that listens to a UDP stream: #[tokio::main] async fn main() { let sock = UdpSocket::bind(address).await.unwrap(); let mut frame = UdpFramed::new(sock, PacketCodec::new()); loop { match…
Yuval Adam
  • 161,610
  • 92
  • 305
  • 395
3
votes
1 answer

How do i run a future without awaiting it? (in rust)

I have some async function async fn get_player(name: String, i: Instant) -> Option { // some code here that returns a player structs } in my main function i want to run the above function concurrently in a loop, this function takes about…
SomeOnionGamer
  • 203
  • 1
  • 4
  • 8
3
votes
2 answers

When the tokio runtime is shutdown, will tasks created with tokio::spawn be dropped as well in Rust?

I'm testing how tasks created with tokio::spawn behave when the main thread terminates. According to tokio docs, all tasks are dropped when the runtime is shutdown. There is no guarantee that a spawned task will execute to completion. When a…
yjlee
  • 363
  • 1
  • 14
3
votes
1 answer

Async recursive function that takes a mutex

How do you create an async recursive function that takes a mutex? Rust claims that this code holds a mutex across an await point. However, the value is dropped before the .await. #[async_recursion] async fn f(mutex: &Arc>) { let mut…
Test
  • 962
  • 9
  • 26
3
votes
1 answer

Rust: expected type [X], but found type [X]

What does it mean when Rust complains that two equal types don't match? The following error appears to be comparing the type... fn(&u32) -> impl futures::Future {f} as FnOnce<(&u32,)>>::Output ...to itself. error[E0308]:…
Test
  • 962
  • 9
  • 26
3
votes
1 answer

How to catch a signal in Rust without memory leaks

I noticed memory leaks in my program and tracked it down to the signal handling. Seems crazy that there isn't a leak-free way to do this. I'm not worried about the "still reachable" bytes reported by Valgrind - I'm worried about the "possibly lost"…
3
votes
0 answers

Initiating stop routine from thread in Rust from C++ code

Since this is most likely an XY problem, I will provide you with more background. I am developing a library that starts a tokio::net::TcpListener, waits for incoming connections and processes them. I have implemented a signal handler in a separate…
John Doe
  • 113
  • 1
  • 2
  • 11
3
votes
1 answer

Borrowed value does not live long enough with async funtion

I am new to Rust and trying to make some code async to run a bunch of tasks in parallel. Here is a simplified example: use futures::future::join_all; #[tokio::main] async fn main() { let mut list = Vec::new(); for i in 1..10 { let…
Daniel
  • 794
  • 10
  • 20
3
votes
2 answers

How to run multiple Tokio async tasks in a loop without using tokio::spawn?

I built a LED clock that also displays weather. My program does a couple of different things in a loop, each thing with a different interval: updates the LEDs every 50ms, checks the light level (to adjust the brightness) every 1 second, fetches…
TPReal
  • 1,539
  • 12
  • 26
3
votes
1 answer

How to connect bevy game to externel TCP server using tokios async TcpStream?

I want to send Events between the game client and server and I already got it working, but I do not know how to do it with bevy. I am dependent to use tokios async TcpStream, because I have to be able to split the stream into a OwnedWriteHalf and…
BrunoWallner
  • 417
  • 3
  • 10