You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
71 lines
1.5 KiB
71 lines
1.5 KiB
use super::*;
|
|
use std::{
|
|
num::NonZeroUsize,
|
|
sync::Arc,
|
|
pin::Pin,
|
|
marker::{
|
|
Send,
|
|
},
|
|
};
|
|
use tokio::{
|
|
prelude::*,
|
|
stream::StreamExt,
|
|
sync::{
|
|
Semaphore,
|
|
},
|
|
};
|
|
use futures::future::{
|
|
join_all,
|
|
Future,
|
|
};
|
|
|
|
pub async fn maybe_await<T>(from: Option<T>) -> Option<<T as Future>::Output>
|
|
where T: Future
|
|
{
|
|
if let Some(v) = from {
|
|
Some(v.await)
|
|
} else {
|
|
None
|
|
}
|
|
}
|
|
|
|
pub async fn do_work(process: impl AsRef<str>, file: impl AsRef<str>)
|
|
{
|
|
let process = process.as_ref();
|
|
let file = file.as_ref();
|
|
|
|
match process::contained_spawn(process, std::iter::once(file)).await {
|
|
Ok(output) => {
|
|
|
|
},
|
|
Err(process::Error::Spawning) => {
|
|
|
|
},
|
|
Err(process::Error::Process) => {
|
|
|
|
},
|
|
}
|
|
|
|
}
|
|
|
|
pub async fn work<I,T>(process: String, files: I, children: Option<NonZeroUsize>) -> Result<(), Box<dyn std::error::Error>>
|
|
where I: IntoIterator<Item=T>,
|
|
T: AsRef<str> + Send + 'static
|
|
{
|
|
//let mut stage: stage::Stage<String> = files.into_iter().map(|x| x.into()).collect();
|
|
let semaphore = children.map(|children| Arc::new(Semaphore::new(children.into())));
|
|
let process = Arc::new(process);
|
|
|
|
join_all(files.into_iter()
|
|
.map(|filename| {
|
|
let semaphore = semaphore.clone();
|
|
let process = Arc::clone(&process);
|
|
tokio::spawn(async move {
|
|
let _lock = maybe_await(semaphore.map(|x| x.acquire_owned())).await;
|
|
do_work(&process[..], filename).await;
|
|
})
|
|
})).await;
|
|
|
|
Ok(())
|
|
}
|