I can create a subprocess in Python and suspend it like this:
proc = subprocess.pOpen(['binary', 'arg1', 'arg2'])
# Subprocess was created in a running state
proc_handle = psutil.Process(pid=proc.pid)
proc_handle.suspend()
# Subprocess is now suspended
proc_handle.resume()
# Subprocess is now running
Question: How can I start the subprocess in a suspended state? I understand that very little time will elapse between creating the subprocess and suspending it, but I am interested in a seamless solution. Also, I know I could easily wrap the subprocess in a parent process which will wait for a resume signal before proceeding, but I would like a less hacky, more elegant solution if one exists. Furthermore, I know I could create a class wrapper around the subprocess, that won't create it until I actually want to run it, but then I won't have a PID assigned to it yet and I won't be able to check stats like memory footprint of the unstarted process.
I would love it if I could do this:
cmd = ['binary', 'arg1', 'arg2']
proc = subprocess.pOpen(cmd, autostart=False) # autostart argument does not exist
# Subprocess was created, so it had a PID and has a memory footprint,
# but is suspended and no code had been run
I recognized while I was writing the above example of pOpen with autostart=False that this may not be possible because of how processes are run. If I want to break at the start of the main() function, then some code will have already been executed as part of static initialization. Ideally, the subprocess will be suspended before that step, just after the process memory is created and initialized.
Note: I am particularly interested in solving this issue in a linux environment but a platform-independent solution would be great if it exists.