问题描述
我试图了解多重处理相对于线程.我知道 multiprocessing 可以绕过Global Interpreter Lock,但是还有哪些其他优点,并且 threading 可以做不到同样的事情吗?
I am trying to understand the advantages of multiprocessing over threading. I know that multiprocessing gets around the Global Interpreter Lock, but what other advantages are there, and can threading not do the same thing?
推荐答案
threading
模块使用线程,multiprocessing
模块使用进程.区别在于线程在相同的内存空间中运行,而进程具有单独的内存.这使得在具有多处理的进程之间共享对象更加困难.由于线程使用相同的内存,因此必须采取预防措施,否则两个线程将同时写入同一内存.这就是全局解释器锁的作用.
The threading
module uses threads, the multiprocessing
module uses processes. The difference is that threads run in the same memory space, while processes have separate memory. This makes it a bit harder to share objects between processes with multiprocessing. Since threads use the same memory, precautions have to be taken or two threads will write to the same memory at the same time. This is what the global interpreter lock is for.
生成进程比生成线程要慢一点.
Spawning processes is a bit slower than spawning threads.
这篇关于多处理与线程Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!