python os.fork是否使用相同的python解释器?

编程入门 行业动态 更新时间:2024-10-09 17:22:12
本文介绍了python os.fork是否使用相同的python解释器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我了解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解释器?

本文发布于:2023-07-06 07:45:35,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1047470.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:python   os   fork

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!