https://github.com/bbqsrc/subterm
A library for managing pools of interactive subprocesses
https://github.com/bbqsrc/subterm
Last synced: about 16 hours ago
JSON representation
A library for managing pools of interactive subprocesses
- Host: GitHub
- URL: https://github.com/bbqsrc/subterm
- Owner: bbqsrc
- Created: 2024-12-28T16:21:19.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2025-01-05T15:53:25.000Z (about 1 year ago)
- Last Synced: 2025-02-13T16:56:12.298Z (12 months ago)
- Language: Rust
- Size: 30.3 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Subterm
A Rust library for efficiently managing pools of interactive subprocesses with async support.
[](https://crates.io/crates/subterm)
[](https://docs.rs/subterm)
[](#license)
## Features
- **Efficient Process Pool Management**: Maintain a pool of reusable subprocess instances
- **Async/Await Support**: Built on tokio for asynchronous operation
- **Fair Queue Management**: Uses a channel-based queuing system for fair process allocation
- **Interactive Process Control**: Read from and write to subprocess stdin/stdout
- **Automatic Process Recovery**: Dead processes are automatically replaced
- **Resource Control**: Limit the maximum number of concurrent processes
## Usage
Add this to your `Cargo.toml`:
```toml
[dependencies]
subterm = "0.0.1"
```
### Using the Process Pool
```rust
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(())
}
```
### Direct Subprocess Usage
You can also create and manage subprocesses directly without using the pool:
```rust
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(())
}
```
## License
Licensed under either of:
- Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
at your option.
## Contributing
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.