Difference in behavior between os.fork and multiprocessing.Process

fork, multiprocessing, process, python, python-multiprocessing

Solution

The answer you are looking for is in detail addressed here. There is also an explanation of differences between different OS.

One big issue is that the `fork` system call does not exist on Windows. Therefore, when running a Windows OS you cannot use this method. `multiprocessing` is a higher-level interface to execute a part of the currently running program. Therefore, it - as forking does - creates a copy of your process current state. That is to say, it takes care of the forking of your program for you.

Therefore, if available you could consider `fork()` a lower-level interface to forking a program, and the `multiprocessing` library to be a higher-level interface to forking.

Problem

I have this code : ``` import os pid = os.fork() if pid == 0: os.environ['HOME'] = "rep1" external_function() else: os.environ['HOME'] = "rep2" external_function() ``` and this code : ``` from multiprocessing import Process, Pipe def f(conn): os.environ['HOME'] = "rep1" external_function() conn.send(some_data) conn.close() if __name__ == '__main__': os.environ['HOME'] = "rep2" external_function() parent_conn, child_conn = Pipe() p = Process(target=f, args=(child_conn,)) p.start() print parent_conn.recv() p.join() ``` The `external_function` initializes an external programs by creating the necessary sub-directories in the directory found in the environment variable `HOME`. This function does this work only once in each process. With the first example, which uses `os.fork()`, the directories are created as expected. But with second example, which uses `multiprocessing`, only the directories in `rep2` get created. Why isn't the second example creating directories in both `rep1` and `rep2`?

Original source

Related problems