Search code examples
pythonpopen

Can a Python program find orphan processes that it previously created?


I'm using popen() to create potentially long-running processes in Python. If the parent program dies and then restarts, is there a way to retrieve the previously created processes that are still running? I'd probably have to use start_new_session=True, which I'm not doing now.

Essentially, I want to get a re-constructed instance of a popen object that points to the child process. I suspect it's not possible, since serializing a popen() object doesn't seem to be possible.


Solution

  • First of all, if one of the processes connected with a pipe dies, any read/write from/to that pipe will result in SIGPIPE -- which by default terminates the process. So your orphan process is very likely to die promptly, too, if the parent crashes.


    As per Can I share a file descriptor to another process on linux or are they local to the process?, it's only possible to share a file discriptor between unrelated processes via a UNIX domain socket, and that requires active participation from both processes.

    As others have pointed out, you also won't be able to wait for that process.


    Depending on what you're trying to achieve (e.g. it's a worker process and you don't want to lose its progress), a better way may be to e.g. save the progress at some points (whether by the parent or by the child) and add an ability to restart from a saved state.