2

I need to create spawn off a process in python that allows the calling process to exit while the child is still running. What is an effective way to do this?

Note: I'm running on a UNIX environment.

  • Take a look at this http://stackoverflow.com/questions/881388/what-is-the-reason-for-performing-a-double-fork-when-creating-a-daemon – gahooa Jul 03 '12 at 17:54

1 Answers1

4

Terminating the parent process does not terminate child processes in Unix-like operating systems, so you don't need to do anything special. Just start your subprocesses with subprocess.Popen and terminate the main process. The orphaned processes will automatically be adopted by init.

Sven Marnach
  • 574,206
  • 118
  • 941
  • 841
  • I would think so, but for whatever reason the parent won't exit until that other subprocess completes. There's a php file calling the function, which basically needs to launch off this subprocess and the return some information quickly. – themcementality Jul 03 '12 at 17:51
  • 2
    Sven is right; I just validated it with a quick script consisting of `p = subprocess.Popen(['/bin/sleep', '60'])`. Something else must be at work. – Mattie Jul 03 '12 at 17:55
  • 2
    @themcementality: Are you by any chance using `subprocess.call()`? This will wait for the child to finish. – Sven Marnach Jul 03 '12 at 17:57