Expand description
Streaming and async iteration support
This module provides async streaming capabilities similar to JavaScript’s
async iterators and stream handling in $.stream-utils.mjs.
It mirrors the JavaScript implementation’s behavior for issue #155:
- The stream yields an explicit
OutputChunk::Exit(code)when the process exits, so consumers can observe the exit code from inside the loop. - The stream does not hang forever when the process has exited but a grandchild keeps the stdio pipes open (the readers are drained with a grace period and then aborted).
- The process can be stopped from inside the loop via
OutputStream::kill/OutputStream::kill_with, and abandoning the stream (e.g.break) also stops the process. - The stop signal is configurable via
StreamingRunner::kill_signal(defaultSIGTERM), just like the JavaScriptkillSignaloption.
§Usage
use command_stream::{StreamingRunner, OutputChunk};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let runner = StreamingRunner::new("yes hello");
// Stream output as it arrives
let mut stream = runner.stream();
let mut count = 0;
while let Some(chunk) = stream.next().await {
match chunk {
OutputChunk::Stdout(data) => {
print!("{}", String::from_utf8_lossy(&data));
count += 1;
if count >= 5 {
// Stop the process from inside the loop.
stream.kill();
}
}
OutputChunk::Stderr(data) => {
eprint!("{}", String::from_utf8_lossy(&data));
}
OutputChunk::Exit(code) => {
println!("Process exited with code: {}", code);
break;
}
}
}
Ok(())
}Structs§
- Output
Stream - Stream of output chunks from a process
- Streaming
Runner - A streaming process runner that allows async iteration over output
Enums§
- Output
Chunk - A chunk of output from a streaming process
Traits§
- Async
Iterator - Async iterator trait for output streams
- Into
Stream - Extension trait to convert ProcessRunner into a stream