多处理与线程Python [英] Multiprocessing vs Threading Python

查看:93
本文介绍了多处理与线程Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图了解多重处理相对于

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆