Crates.io | tokio-fork |
lib.rs | tokio-fork |
version | 0.2.1 |
source | src |
created_at | 2022-01-19 14:33:08.170714 |
updated_at | 2022-01-19 17:29:32.166265 |
description | Fork a process and wait the child process asynchronously |
homepage | https://github.com/nanpuyue/tokio-fork |
repository | https://github.com/nanpuyue/tokio-fork |
max_upload_size | |
id | 516775 |
size | 10,553 |
Fork a process and wait the child process asynchronously
use std::io::Result;
use std::process::exit;
use std::thread::sleep;
use std::time::Duration;
use tokio::runtime::Builder;
use tokio_fork::{fork, Fork};
fn main() -> Result<()> {
match unsafe { fork()? } {
Fork::Parent(mut child) => {
// build the runtime with enable_io()
let rt = Builder::new_current_thread().enable_io().build()?;
rt.block_on(async {
let code = child.wait().await?.code().unwrap();
println!(
"This is the parent process, I see the child process (pid: {}) exit with code {code}.",
child.pid()
);
Ok(())
})
}
Fork::Child => {
println!("This is the child process, I will exit with code 1 in 3s.");
sleep(Duration::from_secs(3));
exit(1)
}
}
}
This project is licensed under the MIT license.