Does python os.fork uses the same python interpreter?

后端 未结 3 1099
死守一世寂寞
死守一世寂寞 2021-02-05 17:17

I understand that threads in Python use the same instance of Python interpreter. My question is it the same with process created by os.fork? Or does each process cr

3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-05 18:04

    Whenever you fork, the entire Python process is duplicated in memory (including the Python interpreter, your code and any libraries, current stack etc.) to create a second process - one reason why forking a process is much more expensive than creating a thread.

    This creates a new copy of the python interpreter.

    One advantage of having two python interpreters running is that you now have two GIL's (Global Interpreter Locks), and therefore can have true multi-processing on a multi-core system.

    Threads in one process share the same GIL, meaning only one runs at a given moment, giving only the illusion of parallelism.

提交回复
热议问题