pub struct ProcessStream { /* private fields */ }Expand description
Handle for receiving process output streams
Implementations§
Source§impl ProcessStream
impl ProcessStream
Sourcepub fn new() -> (ProcessStreamWriter, Self)
pub fn new() -> (ProcessStreamWriter, Self)
Create new process stream handler
Sourcepub fn recv(&self) -> Result<Option<StreamChunk>>
pub fn recv(&self) -> Result<Option<StreamChunk>>
Receive next chunk from process streams
Sourcepub fn try_recv(&self) -> Result<Option<StreamChunk>>
pub fn try_recv(&self) -> Result<Option<StreamChunk>>
Try to receive next chunk without blocking
Examples found in repository?
examples/stream_non_blocking.rs (line 45)
14fn main() -> sandbox_rs::Result<()> {
15 let tmp = tempdir().expect("Failed to create temp dir");
16
17 let mut sandbox = SandboxBuilder::new("non-blocking-example")
18 .memory_limit_str("256M")?
19 .cpu_limit_percent(50)
20 .seccomp_profile(SeccompProfile::Unrestricted)
21 .root(tmp.path())
22 .build()?;
23
24 println!("Running process with non-blocking stream reads...\n");
25
26 // Run a process that outputs slowly
27 let (result, stream) = sandbox.run_with_stream(
28 "/bin/bash",
29 &[
30 "-c",
31 "for i in {1..3}; do echo \"Message $i\"; sleep 0.1; done",
32 ],
33 )?;
34
35 println!("Non-blocking polling:");
36
37 let mut received_chunks = 0;
38 let mut polling_attempts = 0;
39 let mut final_exit_code = result.exit_code;
40
41 loop {
42 polling_attempts += 1;
43
44 // Try to read without blocking
45 match stream.try_recv()? {
46 Some(chunk) => match chunk {
47 StreamChunk::Stdout(line) => {
48 println!("[STDOUT] {}", line);
49 received_chunks += 1;
50 }
51 StreamChunk::Stderr(line) => {
52 eprintln!("[STDERR] {}", line);
53 received_chunks += 1;
54 }
55 StreamChunk::Exit {
56 exit_code,
57 signal: _,
58 } => {
59 println!("Process exited with code: {}", exit_code);
60 // Capture the real exit code from the stream
61 final_exit_code = exit_code;
62 break;
63 }
64 },
65 None => {
66 // No data available right now, we could do other work here
67 std::thread::sleep(Duration::from_millis(10));
68 }
69 }
70
71 // Safety limit to prevent infinite loop in case of issues
72 if polling_attempts > 10000 {
73 println!("Safety timeout reached");
74 break;
75 }
76 }
77
78 // Update result with the actual exit code from streaming
79 let mut result = result;
80 result.exit_code = final_exit_code;
81
82 println!("\nStatistics:");
83 println!(" Chunks received: {}", received_chunks);
84 println!(" Polling attempts: {}", polling_attempts);
85 println!(" Exit code: {}", result.exit_code);
86 println!(" Wall time: {} ms", result.wall_time_ms);
87
88 // Check for seccomp errors and return error if found
89 result.check_seccomp_error()?;
90
91 Ok(())
92}Trait Implementations§
Source§impl Default for ProcessStream
impl Default for ProcessStream
Source§impl IntoIterator for ProcessStream
impl IntoIterator for ProcessStream
Auto Trait Implementations§
impl Freeze for ProcessStream
impl RefUnwindSafe for ProcessStream
impl Send for ProcessStream
impl !Sync for ProcessStream
impl Unpin for ProcessStream
impl UnwindSafe for ProcessStream
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more