python os.fork是否使用相同的python解释器? [英] Does python os.fork uses the same python interpreter?
问题描述
我了解Python中的线程使用相同的Python解释器实例.我的问题与os.fork
创建的进程是否相同?还是os.fork
创建的每个进程都有自己的解释器?
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 created by os.fork
has its own interpreter?
推荐答案
每当您进行分叉时,整个Python进程都会在内存中重复(包括 Python解释器,您的代码和任何库,当前堆栈等)来创建第二个进程-派生一个进程比创建一个线程要昂贵得多的原因之一.
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.
这将创建python解释器的新副本.
This creates a new copy of the python interpreter.
运行两个python解释器的一个优点是您现在拥有两个GIL(全局解释器锁),因此可以在多核系统上进行真正的多处理.
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.
一个进程中的线程共享相同的GIL,这意味着在给定的时刻只有一个线程在运行,这只给人以并行的错觉.
Threads in one process share the same GIL, meaning only one runs at a given moment, giving only the illusion of parallelism.
这篇关于python os.fork是否使用相同的python解释器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!