I have a program that will spawn child processes on certain events. I had initially used
Command::new("exe").spawn()?;, which seemed to do what I wanted; but then I noticed that you should call
.wait() on the
Child. This works fine if the child exits quickly, but for a longer-running process, it blocks the main program until it exits.
I could use another thread, but I would need to
join the thread at the end of the function, which would still block the main thread (or I could just make the whole program multi-threaded, but I thought there might be an easier way). Is there a simpler way to spawn a process and forget about it without refactoring my whole program to use multiple threads?
I don't need to have support for anything other than Linux.
You don't have to call
wait immediately. You can wait with that until you're done doing other things.
Alternatively, how about spawning a thread that calls wait, and then not joining the thread?
Yea, I guess that would work, but if the a child process that the spawned thread created was still alive when the main thread exited, the process would be terminated, right?
OTOH the program will probably be run as a fairly long-lived daemon, so that shouldn't be much of a problem in reality.
Returning from main would kill the thread, but not the child.
You could have a secondary thread dedicated to spawning commands (and eventually another thread to wait on those). You would send the commands to that thread via a queue.
This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.