I spawned the child with a Tokio Command. I also piped stdout, stdin and stderr, after that I put the child into a global Mutex and use it in an actix_web route (The method below is getting called from the route). There I get the child out of the global mutex and try to write to the stdin. This works the first time, but because I'm using take()
, the stdin Option is empty after that and it doesn't work anymore. How can I approach this? I tried to use as_mut()
on the stdin, this compiled, but didn't write to the stdin.
Spawning the child:
let child = Command::new(&info.command)
.current_dir(&info.base_folder)
.args(&info.args)
.stdout(Stdio::piped())
.stdin(Stdio::piped())
.stderr(Stdio::piped())
.spawn()?;
if let Some(p) = child.id() {
PROCESSES.lock().await.insert(p, child);
}
Using the stdin:
async fn send(&mut self, message: &str) -> Result<()> {
let mut server_processes = PROCESSES.lock().await;
let child = server_processes.get_mut(&self.pid.unwrap());
if let Some(process) = child {
let process_stdin = process.stdin.take();
if let Some(mut stdin) = process_stdin {
stdin.write(message.as_bytes()).await?;
}
}
Ok(())
}