17

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

jww
  • 97,681
  • 90
  • 411
  • 885
  • The difference is the actual difference between opening a process, and forking it. Have a look at : https://stackoverflow.com/questions/2483041/what-is-the-difference-between-fork-and-thread – Rohi Apr 03 '18 at 10:40
  • 1
    @Rohi Your link explains the difference between threads and processes. It has nothing to do with this question. – melpomene Apr 03 '18 at 10:51
  • If you understand what a process is (Which is fairly straightforward), all you really need is to read what fork does to understand the difference between the two (Which is explained really well in the answers). So I disagree @melpomene – Rohi Apr 03 '18 at 10:53
  • @Rohi What do you mean, difference? `fork` is the system call that creates a process on Unix. – melpomene Apr 03 '18 at 10:54
  • I meant the difference between the non-unix way to the unix way. The non-unix way is very straightforward, so not much explaining is necessary. @melpomene – Rohi Apr 03 '18 at 10:59
  • 1. `Popen` works fine on Unix. 2. `fork` has no parameters. `Popen` has 14. I know which one I'd want to have more explanation. – melpomene Apr 03 '18 at 11:02
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/168109/discussion-between-rohi-and-melpomene). – Rohi Apr 03 '18 at 11:06
  • Possible duplicate of [Difference between subprocess.Popen and os.system](https://stackoverflow.com/q/4813238/608639) and [How does subprocess.call differ from os.system](https://stackoverflow.com/q/6220157/608639), Maybe even [Calling an external command in Python](https://stackoverflow.com/q/89228/608639). – jww Apr 04 '18 at 21:09

3 Answers3

15

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.

DeepSpace
  • 78,697
  • 11
  • 109
  • 154
10

So I read the documentation for you. Results:

  • os.fork only exists on Unix. It creates a child process (by cloning the existing process), but that's all it does. When it returns, you have two (mostly) identical processes, both running the same code, both returning from os.fork (but the new process gets 0 from os.fork while the parent process gets the PID of the child process).

  • subprocess.Popen is more portable (in particular, it works on Windows). It creates a child process, but you must specify another program that the child process should execute. On Unix, it is implemented by calling os.fork (to clone the parent process), then os.execvp (to load the program into the new child process). Because Popen is all about executing a program, it lets you customize the initial environment of the program. You can redirect its standard handles, specify command line arguments, override environment variables, set its working directory, etc. None of this applies to os.fork.

In general, subprocess.Popen is more convenient to use. If you use os.fork, there's a lot you need to handle manually, and it'll only work on Unix systems. On the other hand, if you actually want to clone a process and not execute a new program, os.fork is the way to go.

Hans Ginzel
  • 8,192
  • 3
  • 24
  • 22
melpomene
  • 84,125
  • 8
  • 85
  • 148
  • 7
    `So I read the documentation for you` Snarkiness cost you an upvote there. If you have the answer, just give it without the sarcasm. – mypetlion Apr 04 '18 at 21:27
  • 8
    @mypetlion Where do you see sarcasm here? – melpomene Apr 04 '18 at 22:21
  • 6
    I personally appreciate that people read the documentation for me, and even more, if they mention it is from specification I tend to rely on such answer more. I could be rephrased to not-include sarcasm-like introduction, but I do like the answer and it was useful for me. – Petr May 28 '19 at 09:27
0

Subprocess.popen() spawns a new OS level process.

os.fork() creates another process which will resume at exactly the same place as this one. So within the first loop run, you get a fork after which you have two processes, the "original one" (which gets a pid value of the PID of the child process) and the forked one (which gets a pid value of 0).

  • 3
    "**Subprocess.popen()** spawns a new OS level process." That's not very helpful because that's exactly what `os.fork` does, too. – melpomene Apr 03 '18 at 10:52