Crates.io | subterm |
lib.rs | subterm |
version | 0.0.1 |
created_at | 2024-12-28 16:34:10.779236+00 |
updated_at | 2024-12-28 16:34:10.779236+00 |
description | A library for managing pools of interactive subprocesses |
homepage | |
repository | |
max_upload_size | |
id | 1497603 |
size | 42,354 |
A Rust library for efficiently managing pools of interactive subprocesses with async support.
Add this to your Cargo.toml
:
[dependencies]
subterm = "0.0.1"
use subterm::{SubprocessPool, Result};
use tokio::process::Command;
#[tokio::main]
async fn main() -> Result<()> {
// Create a pool of 4 processes
let pool = SubprocessPool::new(|| {
let mut cmd = Command::new("your_interactive_program");
cmd.arg("--some-flag");
cmd
}, 4).await?;
// Acquire a process from the pool
let mut process = pool.acquire().await?;
// Interact with the process
process.write_line("some input").await?;
let response = process.read_line().await?;
println!("Got response: {}", response);
Ok(())
}
You can also create and manage subprocesses directly without using the pool:
use std::sync::Arc;
use subterm::{Subprocess, Result};
use tokio::process::Command;
#[tokio::main]
async fn main() -> Result<()> {
let mut process = Subprocess::new(|| {
let mut cmd = Command::new("your_interactive_program");
cmd.arg("--some-flag");
cmd
})?;
// Interact with the process
process.write_line("hello").await?;
let response = process.read_line().await?;
println!("Got response: {}", response);
// Read until a specific delimiter
process.write_bytes(b"part1:part2\n").await?;
let response = process.read_bytes_until(b':').await?;
// Close stdin when done
process.close_stdin();
Ok(())
}
Licensed under either of:
at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.