Go to file
Taiki Endo 22eaea7505 Fix redundant import warning
```
error: the item `AsyncRead` is imported redundantly
   --> src/lib.rs:105:18
    |
105 | use futures_io::{AsyncRead, AsyncSeek, AsyncWrite};
    |                  ^^^^^^^^^
106 | use futures_lite::{future, prelude::*, ready};
    |                            ---------- the item `AsyncRead` is already imported here
    |
    = note: `-D unused-imports` implied by `-D warnings`
    = help: to override `-D warnings` add `#[allow(unused_imports)]`

error: the item `AsyncSeek` is imported redundantly
   --> src/lib.rs:105:29
    |
105 | use futures_io::{AsyncRead, AsyncSeek, AsyncWrite};
    |                             ^^^^^^^^^
106 | use futures_lite::{future, prelude::*, ready};
    |                            ---------- the item `AsyncSeek` is already imported here

error: the item `AsyncWrite` is imported redundantly
   --> src/lib.rs:105:40
    |
105 | use futures_io::{AsyncRead, AsyncSeek, AsyncWrite};
    |                                        ^^^^^^^^^^
106 | use futures_lite::{future, prelude::*, ready};
    |                            ---------- the item `AsyncWrite` is already imported here
```
2024-03-03 06:47:18 -08:00
.github ci: Use cargo-hack's --rust-version flag for msrv check 2024-01-07 06:54:59 +09:00
examples Fix clippy::or_fun_call warning in example 2022-07-17 21:37:47 +09:00
src Fix redundant import warning 2024-03-03 06:47:18 -08:00
tests Ignore clippy::needless_range_loop lint in test 2022-07-17 21:39:09 +09:00
.gitignore Initial commit 2020-05-09 21:06:27 +02:00
CHANGELOG.md v1.5.1 2023-11-12 16:38:04 -08:00
Cargo.toml Migrate to Rust 2021 2024-01-07 06:54:59 +09:00
LICENSE-APACHE Initial commit 2020-05-09 21:06:27 +02:00
LICENSE-MIT Initial commit 2020-05-09 21:06:27 +02:00
README.md Clean up CI config 2022-01-08 22:35:02 +09:00

README.md

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.