diff options
author | Lars Wirzenius <liw@liw.fi> | 2021-04-23 10:43:27 +0300 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2021-04-23 10:43:27 +0300 |
commit | 2daa99223ad6b382038acfe2f5646cdd94ff2d10 (patch) | |
tree | 408e4439f95b311f0c1a668ef666ad877fd9d465 | |
parent | 2e76c16b23a52e51a6d8cddf54a6cf966e11a7d3 (diff) | |
download | summain-rs-2daa99223ad6b382038acfe2f5646cdd94ff2d10.tar.gz |
use spawn_blocking, but not async in blocking task
-rw-r--r-- | src/bin/summain.rs | 15 | ||||
-rw-r--r-- | src/lib.rs | 16 |
2 files changed, 14 insertions, 17 deletions
diff --git a/src/bin/summain.rs b/src/bin/summain.rs index ec17d66..8ec14f8 100644 --- a/src/bin/summain.rs +++ b/src/bin/summain.rs @@ -6,6 +6,7 @@ use summain::ManifestEntry; fn main() -> anyhow::Result<()> { let runtime = tokio::runtime::Builder::new_multi_thread() .worker_threads(16) + .max_blocking_threads(16) .build()?; runtime.block_on(async { async_main().await })?; Ok(()) @@ -17,11 +18,11 @@ async fn async_main() -> anyhow::Result<()> { let mut handles = vec![]; for filename in opt.pathnames.iter().cloned() { - handles.push(tokio::spawn(async move { report(filename) })); + handles.push(tokio::task::spawn_blocking(move || report(filename))); } for h in handles { - h.await?.await?; + h.await??; } Ok(()) @@ -33,14 +34,12 @@ struct Opt { pathnames: Vec<PathBuf>, } -async fn report(path: PathBuf) -> anyhow::Result<()> { - let m = manifest(&path).await?; +fn report(path: PathBuf) -> anyhow::Result<()> { + let m = manifest(&path)?; print!("{}", serde_yaml::to_string(&m)?); Ok(()) } -async fn manifest(path: &Path) -> anyhow::Result<ManifestEntry> { - ManifestEntry::new(path) - .await - .with_context(|| format!("{}", path.display())) +fn manifest(path: &Path) -> anyhow::Result<ManifestEntry> { + ManifestEntry::new(path).with_context(|| format!("{}", path.display())) } @@ -31,12 +31,11 @@ use serde::Serialize; use sha2::{Digest, Sha256}; +use std::fs::File; use std::fs::{read_link, symlink_metadata}; +use std::io::{BufReader, Read}; use std::os::linux::fs::MetadataExt; use std::path::{Path, PathBuf}; -use tokio::fs::File; -use tokio::io::AsyncReadExt; -use tokio::io::BufReader; const BUF_SIZE: usize = 1024 * 1024; @@ -61,10 +60,10 @@ impl ManifestEntry { /// caller. This function doesn't query the system for it. /// /// The structure can be serialized using serde. - pub async fn new(path: &Path) -> std::io::Result<Self> { + pub fn new(path: &Path) -> std::io::Result<Self> { let m = symlink_metadata(path)?; let hash = if m.is_file() { - Some(file_checksum(path).await?) + Some(file_checksum(path)?) } else { None }; @@ -86,19 +85,18 @@ impl ManifestEntry { } } -async fn file_checksum(path: &Path) -> std::io::Result<String> { +fn file_checksum(path: &Path) -> std::io::Result<String> { let mut hasher = Sha256::new(); - let file = File::open(path).await?; + let file = File::open(path)?; let mut reader = BufReader::new(file); let mut buf = vec![0; BUF_SIZE]; loop { - let n = reader.read(&mut buf).await?; + let n = reader.read(&mut buf)?; if n == 0 { break; } hasher.update(&buf[..n]); - tokio::task::yield_now().await; } let hash = hasher.finalize(); Ok(format!("{:x}", hash)) |