Depending on the platform,
multiprocessing
supports three ways to start a process. These start methods arespawn
The parent process starts a fresh python interpreter process. The child process will only inherit those resources necessary to run the process object’s
run()
method. In particular, unnecessary file descriptors and handles from the parent process will not be inherited. Starting a process using this method is rather slow compared to using fork or forkserver.Available on Unix and Windows. The default on Windows and macOS.
fork
The parent process uses
os.fork()
to fork the Python interpreter. The child process, when it begins, is effectively identical to the parent process. All resources of the parent are inherited by the child process. Note that safely forking a multithreaded process is problematic.Available on Unix only. The default on Unix.
forkserver
When the program starts and selects the forkserver start method, a server process is started. From then on, whenever a new process is needed, the parent process connects to the server and requests that it fork a new process. The fork server process is single threaded so it is safe for it to use
os.fork()
. No unnecessary resources are inherited.Available on Unix platforms which support passing file descriptors over Unix pipes.
multiprocess有3种方式,在windows默认用且只能用spawn方式,在linux默认用fork方式。
spawn方式会重新运行代码,父进程的环境不会自动继承过来。
fork方式会将父进程的环境继承过来。
因此经常会出现在linux开发的多进程跑的没问题,但是在windows里会报错说这没有那没有的报错。