IPython並行計算工具
解決並行計算和分布式計算的問題
-
運行解釋說明
- 一直以來
Python的並發問題都會被大家所詬病,正是因為全局解釋鎖的存在,導致其不能夠真正的做到並發的執行。所以,我們就需要ipyparallel的存在來幫助我們處理並發計算的問題了。 - 在
ipyparallel中,可以利用多個engine同時運行一個任務來加快處理的速度。集群被抽象為view,包括direct_view和balanced_view。其中,direct_view是所有的engine的抽象,當然也可以自行指定由哪些engine構成,而balanced_view是多個engine經過負載均衡之后,抽象出來的由“單一”engine構成的view。利用ipyparallel並行化的基本思路是將要處理的數據首先進行切分,然后分布到每一個engine上,然后將最終的處理結果合並,得到最終的結果,其思路和mapreduce類似。
- 一直以來
-
並行計算分類
- ipcluster - 單機並行計算
- ipyparallel - 分布式計算
-
相關連接地址
-
安裝方式
bash
# 使用pip安裝 $ pip install ipyparallel
- 配置並行環境
bash
# 命令可以簡單的創建一個通用的並行環境profile配置文件 $ ipython profile create --parallel --profile=myprofile
1. 並行計算示例
做一次
wordcount的計算測試。
- 數據來源地址
bash
# 使用wget下載 $ wget http://www.gutenberg.org/files/27287/27287-0.txt
- 不並行的版本
python
In [1]: import re In [2]: import io In [3]: from collections import defaultdict In [4]: non_word = re.compile(r'[\W\d]+', re.UNICODE) In [5]: common_words = { ...: 'the','of','and','in','to','a','is','it','that','which','as','on','by', ...: 'be','this','with','are','from','will','at','you','not','for','no','have', ...: 'i','or','if','his','its','they','but','their','one','all','he','when', ...: 'than','so','these','them','may','see','other','was','has','an','there', ...: 'more','we','footnote', 'who', 'had', 'been', 'she', 'do', 'what', ...: 'her', 'him', 'my', 'me', 'would', 'could', 'said', 'am', 'were', 'very', ...: 'your', 'did', 'not', ...: } In [6]: def yield_words(filename): ...: import io ...: with io.open(filename, encoding='latin-1') as f: ...: for line in f: ...: for word 