pub struct Process { /* private fields */ }Expand description
A builder for configuring and spawning a process.
This provides an ergonomic API for spawning processes while keeping the stream type (broadcast vs single subscriber) explicit at the spawn callsite.
§Examples
use tokio_process_tools::*;
use tokio::process::Command;
// Simple case with auto-derived name
let process = Process::new(Command::new("ls"))
.spawn_broadcast()?;
// With explicit name (no allocation when using string literal)
let process = Process::new(Command::new("server"))
.name("my-server")
.spawn_single_subscriber()?;
// With custom capacities
let process = Process::new(Command::new("cargo"))
.name("test-runner")
.stdout_capacity(512)
.stderr_capacity(512)
.spawn_broadcast()?;Implementations§
Source§impl Process
impl Process
Sourcepub fn new(cmd: Command) -> Self
pub fn new(cmd: Command) -> Self
Creates a new process builder from a tokio command.
If no name is explicitly set via Process::name, the name will be auto-derived
from the command’s program name.
§Examples
use tokio_process_tools::*;
use tokio::process::Command;
let process = Process::new(Command::new("ls"))
.spawn_broadcast()?;Sourcepub fn name(self, name: impl Into<ProcessName>) -> Self
pub fn name(self, name: impl Into<ProcessName>) -> Self
Sets how the process should be named.
You can provide either an explicit name or configure automatic name generation. The name is used for logging and debugging purposes.
§Examples
use tokio_process_tools::*;
use tokio::process::Command;
// Explicit name
let process = Process::new(Command::new("server"))
.name(ProcessName::Explicit("my-server".into()))
.spawn_broadcast()?;
// Auto-generated with arguments
let mut cmd = Command::new("cargo");
cmd.arg("test");
let process = Process::new(cmd)
.name(ProcessName::Auto(AutoName::Using(AutoNameSettings::program_with_args())))
.spawn_broadcast()?;Sourcepub fn with_name(self, name: impl Into<Cow<'static, str>>) -> Self
pub fn with_name(self, name: impl Into<Cow<'static, str>>) -> Self
Convenience method to set an explicit process name.
This is a shorthand for .name(ProcessName::Explicit(...)).
§Examples
use tokio_process_tools::*;
use tokio::process::Command;
// Static name (no allocation)
let process = Process::new(Command::new("server"))
.with_name("my-server")
.spawn_broadcast()?;
// Dynamic name (allocates)
let id = 42;
let process = Process::new(Command::new("worker"))
.with_name(format!("worker-{id}"))
.spawn_single_subscriber()?;Sourcepub fn with_auto_name(self, mode: AutoName) -> Self
pub fn with_auto_name(self, mode: AutoName) -> Self
Convenience method to configure automatic name generation.
This is a shorthand for .name(ProcessName::Auto(...)).
§Examples
use tokio_process_tools::*;
use tokio::process::Command;
let mut cmd = Command::new("server");
cmd.arg("--database").arg("sqlite");
cmd.env("S3_ENDPOINT", "127.0.0.1:9000");
let process = Process::new(cmd)
.with_auto_name(AutoName::Using(AutoNameSettings::program_with_env_and_args()))
.spawn_broadcast()?;Sourcepub fn stdout_chunk_size(self, chunk_size: NumBytes) -> Self
pub fn stdout_chunk_size(self, chunk_size: NumBytes) -> Self
Sets the stdout chunk size.
This controls the size of the buffer used when reading from the process’s stdout stream. Default is DEFAULT_CHUNK_SIZE.
§Examples
use tokio_process_tools::*;
use tokio::process::Command;
let process = Process::new(Command::new("server"))
.stdout_chunk_size(32.kilobytes())
.spawn_broadcast()?;Sourcepub fn stderr_chunk_size(self, chunk_size: NumBytes) -> Self
pub fn stderr_chunk_size(self, chunk_size: NumBytes) -> Self
Sets the stderr chunk size.
This controls the size of the buffer used when reading from the process’s stderr stream. Default is DEFAULT_CHUNK_SIZE.
§Examples
use tokio_process_tools::*;
use tokio::process::Command;
let process = Process::new(Command::new("server"))
.stderr_chunk_size(32.kilobytes())
.spawn_broadcast()?;Sourcepub fn chunk_sizes(self, chunk_size: NumBytes) -> Self
pub fn chunk_sizes(self, chunk_size: NumBytes) -> Self
Sets the stdout and stderr chunk sizes.
This controls the size of the buffers used when reading from the process’s stdout and stderr streams. Default is DEFAULT_CHUNK_SIZE.
§Examples
use tokio_process_tools::*;
use tokio::process::Command;
let process = Process::new(Command::new("server"))
.chunk_sizes(32.kilobytes())
.spawn_broadcast()?;Sourcepub fn stdout_capacity(self, capacity: usize) -> Self
pub fn stdout_capacity(self, capacity: usize) -> Self
Sets the stdout channel capacity.
This controls how many chunks can be buffered before backpressure is applied. Default is DEFAULT_CHANNEL_CAPACITY.
§Examples
use tokio_process_tools::*;
use tokio::process::Command;
let process = Process::new(Command::new("server"))
.stdout_capacity(512)
.spawn_broadcast()?;Sourcepub fn stderr_capacity(self, capacity: usize) -> Self
pub fn stderr_capacity(self, capacity: usize) -> Self
Sets the stderr channel capacity.
This controls how many chunks can be buffered before backpressure is applied. Default is DEFAULT_CHANNEL_CAPACITY.
§Examples
use tokio_process_tools::*;
use tokio::process::Command;
let process = Process::new(Command::new("server"))
.stderr_capacity(256)
.spawn_broadcast()?;Sourcepub fn capacities(self, capacity: usize) -> Self
pub fn capacities(self, capacity: usize) -> Self
Sets the stdout and stderr channel capacity.
This controls how many chunks can be buffered before backpressure is applied. Default is DEFAULT_CHANNEL_CAPACITY.
§Examples
use tokio_process_tools::*;
use tokio::process::Command;
let process = Process::new(Command::new("server"))
.capacities(256)
.spawn_broadcast()?;Sourcepub fn spawn_broadcast(
self,
) -> Result<ProcessHandle<BroadcastOutputStream>, SpawnError>
pub fn spawn_broadcast( self, ) -> Result<ProcessHandle<BroadcastOutputStream>, SpawnError>
Spawns the process with broadcast output streams.
Broadcast streams support multiple concurrent consumers of stdout/stderr, which is useful when you need to inspect, collect, and process output simultaneously. This comes with slightly higher memory overhead due to cloning.
§Examples
use tokio_process_tools::*;
use tokio::process::Command;
let mut process = Process::new(Command::new("ls"))
.spawn_broadcast()?;
// Multiple consumers can read the same output
let _logger = process.stdout().inspect_lines(|line| {
println!("{}", line);
tokio_process_tools::Next::Continue
}, Default::default());
let _collector = process.stdout().collect_lines_into_vec(Default::default());Sourcepub fn spawn_single_subscriber(
self,
) -> Result<ProcessHandle<SingleSubscriberOutputStream>, SpawnError>
pub fn spawn_single_subscriber( self, ) -> Result<ProcessHandle<SingleSubscriberOutputStream>, SpawnError>
Spawns the process with single subscriber output streams.
Single subscriber streams are more efficient (lower memory, no cloning) but only allow one consumer of stdout/stderr at a time. Use this when you only need to either inspect OR collect output, not both simultaneously.
§Examples
use tokio_process_tools::*;
use tokio::process::Command;
let process = Process::new(Command::new("ls"))
.spawn_single_subscriber()?;
// Only one consumer allowed
let collector = process.stdout().collect_lines_into_vec(Default::default());