Difference in behavior between os.fork and multiprocessing.Process

前端 未结 2 2095
小蘑菇
小蘑菇 2021-02-09 02:43

I have this code :

import os

pid = os.fork()

if pid == 0:
    os.environ[\'HOME\'] = \"rep1\"
    external_function()
else:
    os.environ[\'HOME\'] = \"rep2\"         


        
2条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-09 03:37

    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.

提交回复
热议问题