Python 3.4中的多處理與多線程與asyncio

[英]multiprocessing vs multithreading vs asyncio in Python 3.4

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

我發現在Python 3.4中,幾乎沒有用於多處理/線程的不同庫:多處理與線程和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.


1 个解决方案



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.

它們旨在用於(略微)不同的目的和/或要求。 CPython(典型的主線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.

asyncio(這種技術不僅可以在Python中使用,其他語言和/或框架也可以使用它,例如Boost.ASIO)是一種從需要並行代碼的許多同時源有效處理大量I / O操作的方法執行。所以它只是一個特定任務的解決方案(確實是一個好的!),而不是一般的並行處理。



粤ICP备14056181号  © 2014-2021