Skip to content

Commit

Permalink
replace rayon with tokio
Browse files Browse the repository at this point in the history
  • Loading branch information
Mon-ius committed Mar 4, 2024
1 parent cf181e4 commit 95a5501
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 7 deletions.
4 changes: 2 additions & 2 deletions ato-cli/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
[package]
name = "ato-cli"
version = "1.0.1"
version = "1.0.2"
edition = "2021"

[dependencies]
ato = { path = "../ato" }
clap = { version= "4.5.1", features=["derive"] }

tokio = { version = "1.36.0", features = ["full"] }

[[bin]]
name = "ato-cli"
Expand Down
8 changes: 5 additions & 3 deletions ato-cli/src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use clap::Parser;
use libato::ry::process;
// use libato::ry::process;
use libato::tk::process;

#[derive(Parser, Debug)]
struct Args {
Expand All @@ -9,10 +10,11 @@ struct Args {
external: String,
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let _args = Args::parse();
let start = std::time::Instant::now();
let _ = process(&_args.dataset, &_args.external);
let _ = process(&_args.dataset, &_args.external).await?;
let duration = start.elapsed();
println!("Time elapsed in process() is: {:?}", duration);
Ok(())
Expand Down
3 changes: 2 additions & 1 deletion ato/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ version = "0.1.0"
edition = "2021"

[dependencies]
rayon = "1.9.0"
tokio = { version = "1.36.0", features = ["full"] }
# rayon = "1.9.0"
walkdir="2.5.0"
md5 = "0.7.0"

Expand Down
3 changes: 2 additions & 1 deletion ato/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod ry;
// pub mod ry;
pub mod tk;
55 changes: 55 additions & 0 deletions ato/src/tk.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@

use walkdir::WalkDir;
use tokio::{fs, task};
use std::path;

pub async fn async_hash(r: &str, p: &path::Path, e: &str) -> Result<(), Box<dyn std::error::Error>>{
let _dir = p.with_file_name("");
let _num = _dir.file_name().unwrap().to_str().unwrap();
let _in = p.with_file_name(format!("{}_{}_{}", _num, "total", e));
let _up = p.with_file_name(format!("{}_{}_{}", _num, "up", e));
let _low = p.with_file_name(format!("{}_{}_{}", _num, "low", e));
if _in.is_file() && _up.is_file() && _low.is_file() {
let contents = fs::read(&_in).await?;
let digest = md5::compute(&contents);
let md5_dir = format!("{}/{:x}", r, digest);

let _ = fs::create_dir_all(&md5_dir).await?;
let _ = fs::copy(_in, format!("{}/in.stl", &md5_dir)).await?;
let _ = fs::copy(_up, format!("{}/up.stl", &md5_dir)).await?;
let _ = fs::copy(_low, format!("{}/low.stl", &md5_dir)).await?;
let _ = fs::copy(p, format!("{}/gt.stl", &md5_dir)).await?;
}
else {
println!("{:#?}", p);
}
Ok(())
}

pub async fn process(_pth: &str, e:&str) -> Result<(), Box<dyn std::error::Error>> {
let root = format!("{}_sha", _pth.to_owned());
let _ = fs::create_dir_all(&root).await?;
let mut pool = vec![];

let pdd: Vec<path::PathBuf> =
WalkDir::new(_pth).into_iter()
.map(|e| e.unwrap().into_path())
.filter(|c| {
c.display().to_string().contains("gt")
})
.collect();

for p in pdd {
let _root = root.clone();
let _e = e.to_string();
pool.push(task::spawn(async move {
let _ = async_hash(&_root, &p, &_e).await;
}));
}

for task in pool {
task.await?;
}

Ok(())
}

0 comments on commit 95a5501

Please sign in to comment.