Search code examples
pythonfork

What is the difference between subprocess.Popen() and os.fork()?


It seems like subprocess.Popen() and os.fork() both are able to create a child process. I would however like to know what the difference is between both. When would you use which one? I tried looking at their source code but I couldn't find fork()'s source code on my machine and it wasn't totally clear how Popen works on Unix machines.

Could someobody please elaborate?

Thanks


Solution

  • subprocess.Popen let's you execute an arbitrary program/command/executable/whatever in its own process.

    os.fork only allows you to create a child process that will execute the same script from the exact line in which you called it. As its name suggests, it "simply" forks the current process into 2.

    os.fork is only available on Unix, and subprocess.Popen is cross-platfrom.