Rust library for simple yet powerful events based multi process execution(blocking & non-blocking modes) in parallel and with data streaming
Run a process based on the provided process request.
Generates various events [ProcessEvent
] according to the process's life-cycle, process's information and data [ProcessData
] associated with that event
# Arguments
ProcessRequest : [ProcessRequest
] // A request structure to start a process
# Return
For Blocking mode it returns [None
] and for Non-Blocking mode it will return [Some(io::Result<JoinHandle<()>>)
], so the caller can join & wait for the process completion if needed!
# Examples
```
// Setup callback (for the process events and data streaming)
//
use processeventsstreaming::{ProcessRequest, ProcessResult, ProcessData, ProcessEvent};
use std::{thread, time::Duration};
let callback = |status: &ProcessEvent, data: &ProcessData| -> ProcessResult {
match status {
ProcessEvent::Started => {
println!(
"Event {:?} | req-id {} | Pids: {:?}",
status,
data.request.asref().unwrap().requestid,
data.childpids()
);
}
ProcessEvent::IOData => {
println!(
"Event {:?} | req-id {} | # {} : {}",
status,
data.request.asref().unwrap().requestid,
data.linenumber,
data.line
);
//now assume we want to exit the process with some data
let mut result = ProcessResult::new();
result.setexitflagandsuccess(true, Ok(true));
result.datanum = Some(8111981);
result.datavec_str = Some(vec![String::from("I found my hidden data!")]);
return result;
//demo how to kill/stop
//_ = data.kill();
}
other => {
if !data.line.is_empty() {
println!(
"Event {:?} | req-id {} | additional detail(s): {}",
other,
data.request.as_ref().unwrap().request_id,
data.line
);
} else {
println!(
"Event {:?} | req-id {}",
other,
data.request.as_ref().unwrap().request_id
);
}
}
}
ProcessResult::new()
};
let request2 = ProcessRequest {
request_id: 151,
callback: Some(Arc::new(callback)),
use_shell: true,
cmd_line: vec![vec![
String::from("echo"),
String::from("stdout"),
String::from("&"),
String::from("echo"),
String::from("stderr"),
String::from(">&2"),
]],
non_blocking_mode: true,
};
// non Blocking mode
let process_result = ProcessRequest::start(request2);
println!("Returned from Start! of non blocking");
let mut internal_data = ProcessResult::new();
//check & wait for the non blocking mode
if process_result.join_handle.is_some() {
if process_result.join_handle.as_ref().unwrap().is_ok() {
internal_data = process_result.join_handle.unwrap().unwrap().join().unwrap();
println!(
"Start - join waiting over in non blocking mode {:?}",
internal_data
);
} else {
internal_data.success = Err(process_result.join_handle.unwrap().err().unwrap());
println!(
"Start - Error in non blocking mode {:?}",
internal_data.success
);
}
} else {
internal_data = process_result;
}
println!("result dump : {:?}", internal_data);
println!(
"test_using_sh_output_streaming, start calc in windows {:?}",
ProcessRequest::start(ProcessRequest {
request_id: 191,
callback: Some(Arc::new(callback)),
use_shell: true,
cmd_line: vec![vec![String::from("calc")]],
non_blocking_mode: false,
}));
```
File [../src/lib.rs
] contains examples in detail under #[test]
Sample output of the process events from the tests
``` running 2 tests Event Starting | req-id 151 | additional detail(s): Executing in thread-context -> id: ThreadId(2), name: Some("tests::testusingshoutputstreamingnewversion") Event Started | req-id 151 | Pids: [14120] Event IOData | req-id 151 | # 1 : stdout
Event ExitRequested | req-id 151 | additional detail(s): stdout
Event Exited | req-id 151 Returned from Start! of non blocking result dump : ProcessResult { joinhandle: None, shouldexit: Some(true), success: Ok(true), datavecstr: Some(["I found my hidden data!"]), databool: None, datanum: Some(8111981), data_decimal: None }
Event Starting | req-id 105 | additional detail(s): Executing in thread-context -> id: ThreadId(3), name: Some("pesthrq105")
Event Started | req-id 105 | Pids: [10332, 864]
Event IOData | req-id 105 | # 1 :
Event IOData | req-id 105 | # 2 :
Event IOData | req-id 105 | # 3 : 5 Dir(s) 679,760,900,096 bytes free
Event IOData | req-id 105 | # 4 : 7 File(s) 8,018 bytes
Event IOData | req-id 105 | # 5 : Directory of C:\process-events-streaming
Event IOData | req-id 105 | # 6 : 04-11-2022 01:53 PM 210 rustfmt.toml
Event IOData | req-id 105 | # 7 : 04-11-2022 04:58 PM 682 Cargo.toml
Event IOData | req-id 105 | # 8 : 04-11-2022 05:09 PM
Event Starting | req-id 106 | additional detail(s): Executing in thread-context -> id: ThreadId(3), name: Some("pesthrq106") Event Started | req-id 106 | Pids: [25936] Event IOData | req-id 106 | # 1 : \"Sandy\" Event IOEof | req-id 106 Event Exited | req-id 106 testusingshoutput_streaming, demo double quotes ()
Event StartError | req-id 107 | additional detail(s): "Command line - arguments are unavailable!" testusingshoutputstreaming, no arguments means start error ()
```
Licensed under
This library is using a wonderful library 'duct' for low level process handling