Crates.io | process-stream |
lib.rs | process-stream |
version | |
source | src |
created_at | 2022-05-21 19:18:02.495272+00 |
updated_at | 2025-05-08 10:17:27.761431+00 |
description | Thin wrapper around [`tokio::process`] to make it streamable |
homepage | https://github.com/kkharji/process-stream |
repository | https://github.com/kkharji/process-stream |
max_upload_size | |
id | 590825 |
Cargo.toml error: | TOML parse error at line 21, column 1 | 21 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include` |
size | 0 |
Wraps tokio::process::Command
to future::stream
.
This library provide ProcessExt to create your own custom process
process-stream = "0.3.1"
Vec<String>
or Vec<&str>
use process_stream::{Process, ProcessExt, StreamExt};
use std::io;
#[tokio::main]
async fn main() -> io::Result<()> {
let mut ls_home: Process = vec!["/bin/ls", "."].into();
let mut stream = ls_home.spawn_and_stream()?;
while let Some(output) = stream.next().await {
println!("{output}")
}
Ok(())
}
Path/PathBuf/str
use process_stream::{Process, ProcessExt, StreamExt};
use std::io;
#[tokio::main]
async fn main() -> io::Result<()> {
let mut process: Process = "/bin/ls".into();
// block until process completes
let outputs = process.spawn_and_stream()?.collect::<Vec<_>>().await;
println!("{outputs:#?}");
Ok(())
}
use process_stream::{Process, ProcessExt, StreamExt};
use std::io;
#[tokio::main]
async fn main() -> io::Result<()> {
let mut ls_home = Process::new("/bin/ls");
ls_home.arg("~/");
let mut stream = ls_home.spawn_and_stream()?;
while let Some(output) = stream.next().await {
println!("{output}")
}
Ok(())
}
use process_stream::{Process, ProcessExt, StreamExt};
use std::io;
#[tokio::main]
async fn main() -> io::Result<()> {
let mut long_process = Process::new("cat");
let mut stream = long_process.spawn_and_stream()?;
tokio::spawn(async move {
while let Some(output) = stream.next().await {
println!("{output}")
}
});
// process some outputs
tokio::time::sleep(std::time::Duration::from_secs(2)).await;
// close the process
long_process.abort();
Ok(())
}
use process_stream::{Process, ProcessExt, StreamExt};
use tokio::io::AsyncWriteExt;
use std::process::Stdio;
#[tokio::main]
async fn main() -> std::io::Result<()> {
let mut process: Process = Process::new("sort");
// Set stdin (by default is set to null)
process.stdin(Stdio::piped());
// Get Stream;
let mut stream = process.spawn_and_stream().unwrap();
// Get writer from stdin;
let mut writer = process.take_stdin().unwrap();
// Spawn new async task and move stream to it
let reader_thread = tokio::spawn(async move {
while let Some(output) = stream.next().await {
if output.is_exit() {
println!("DONE")
} else {
println!("{output}")
}
}
});
// Spawn new async task and move writer to it
let writer_thread = tokio::spawn(async move {
writer.write(b"b\nc\na\n").await.unwrap();
writer.write(b"f\ne\nd\n").await.unwrap();
});
// Wait till all threads finish
writer_thread.await.unwrap();
reader_thread.await.unwrap();
// Result
// a
// b
// c
// d
// e
// f
// DONE
Ok(())
}