Python 3.4中的多处理与多线程与异步 [英] multiprocessing vs multithreading vs asyncio in Python 3.4

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

问题描述

我发现在Python 3.4中,用于处理/线程处理的库很少:多重处理线程

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中可用,其他语言和/或框架也有此技术,例如

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

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