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 个解决方案

#1


31  

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操作的方法執行。所以它只是一個特定任務的解決方案(確實是一個好的!),而不是一般的並行處理。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2014/12/12/7210d457221a9448101c5cd99520cb01.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com