#file-io #thread-pool #async #stdin #file #stdio #process

blocking

A thread pool for isolating blocking I/O in async programs

29 releases (11 stable)

1.6.0 Apr 27, 2024
1.5.1 Nov 13, 2023
1.4.1 Oct 8, 2023
1.3.1 Apr 7, 2023
0.1.0 Jun 15, 2019

#53 in Asynchronous

Download history 463331/week @ 2024-01-24 487767/week @ 2024-01-31 384639/week @ 2024-02-07 383930/week @ 2024-02-14 404701/week @ 2024-02-21 423875/week @ 2024-02-28 421433/week @ 2024-03-06 417892/week @ 2024-03-13 436457/week @ 2024-03-20 437502/week @ 2024-03-27 464753/week @ 2024-04-03 469411/week @ 2024-04-10 470729/week @ 2024-04-17 467207/week @ 2024-04-24 443629/week @ 2024-05-01 364786/week @ 2024-05-08

1,835,065 downloads per month
Used in 3,739 crates (61 directly)

Apache-2.0 OR MIT

43KB
508 lines

blocking

Build License Cargo Documentation

A thread pool for isolating blocking I/O in async programs.

Sometimes there's no way to avoid blocking I/O. Consider files or stdin, which have weak async support on modern operating systems. While IOCP, AIO, and io_uring are possible solutions, they're not always available or ideal.

Since blocking is not allowed inside futures, we must move blocking I/O onto a special thread pool provided by this crate. The pool dynamically spawns and stops threads depending on the current number of running I/O jobs.

Note that there is a limit on the number of active threads. Once that limit is hit, a running job has to finish before others get a chance to run. When a thread is idle, it waits for the next job or shuts down after a certain timeout.

The default number of threads (set to 500) can be altered by setting BLOCKING_MAX_THREADS environment variable with value between 1 and 10000.

Examples

Read the contents of a file:

use blocking::unblock;
use std::fs;

let contents = unblock(|| fs::read_to_string("file.txt")).await?;
println!("{}", contents);

Read a file and pipe its contents to stdout:

use blocking::{unblock, Unblock};
use futures_lite::io;
use std::fs::File;

let input = unblock(|| File::open("file.txt")).await?;
let input = Unblock::new(input);
let mut output = Unblock::new(std::io::stdout());

io::copy(input, &mut output).await?;

Iterate over the contents of a directory:

use blocking::Unblock;
use futures_lite::prelude::*;
use std::fs;

let mut dir = Unblock::new(fs::read_dir(".")?);
while let Some(item) = dir.next().await {
    println!("{}", item?.file_name().to_string_lossy());
}

Spawn a process:

use blocking::unblock;
use std::process::Command;

let out = unblock(|| Command::new("dir").output()).await?;

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~1MB
~15K SLoC