Python 3 中的多处理、多线程和异步

multiprocessing vs multithreading vs asyncio in Python 3(Python 3 中的多处理、多线程和异步)

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

问题描述

我发现在 Python 3.4 中有几个不同的多处理/线程库:multiprocessing vs 线程 vs asyncio.

I found that in Python 3.4 there are few different libraries for multiprocessing/threading: multiprocessing vs threading vs asyncio.

但我不知道该使用哪一个或者是推荐的".他们做同样的事情,还是不同?如果是这样,哪一个用于什么?我想在我的计算机上编写一个使用多核的程序.但我不知道我应该学习哪个库.

But I don't know which one to use or is the "recommended one". Do they do the same thing, or are different? If so, which one is used for what? I want to write a program that uses multicores in my computer. But I don't know which library I should learn.

推荐答案

它们用于(稍微)不同的目的和/或要求.CPython(一个典型的主线 Python 实现)仍然具有 全局解释器锁,因此是一个多线程应用程序(现在实现并行处理的标准方法)不是最理想的.这就是为什么 multiprocessing 可能 优于 threading 的原因.但并不是每个问题都可以有效地分解为[几乎独立的]部分,因此可能需要繁重的进程间通信.这就是为什么 multiprocessing 通常可能不优于 threading.

They are intended for (slightly) different purposes and/or requirements. CPython (a typical, mainline Python implementation) still has the global interpreter lock so a multi-threaded application (a standard way to implement parallel processing nowadays) is suboptimal. That's why multiprocessing may be preferred over threading. But not every problem may be effectively split into [almost independent] pieces, so there may be a need in heavy interprocess communications. That's why multiprocessing may not be preferred over threading in general.

asyncio(这种技术不仅在 Python 中可用,其他语言和/或框架也有它,例如 Boost.ASIO) 是一种有效处理来自许多同时源的大量 I/O 操作而无需并行代码执行的方法.所以它只是针对特定任务的解决方案(确实是一个很好的解决方案!),而不是一般的并行处理.

asyncio (this technique is available not only in Python, other languages and/or frameworks also have it, e.g. Boost.ASIO) is a method to effectively handle a lot of I/O operations from many simultaneous sources w/o need of parallel code execution. So it's just a solution (a good one indeed!) for a particular task, not for parallel processing in general.

这篇关于Python 3 中的多处理、多线程和异步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Python 3 中的多处理、多线程和异步

基础教程推荐