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

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

问题描述

我试图了解 multiprocessing 相对于 线程.我知道多处理绕过了全局解释器锁,但还有什么其他优势,线程不能做同样的事情吗?

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天全站免登陆