IPython并行计算工具

 


解决并行计算和分布式计算的问题

  • 运行解释说明

    • 一直以来Python的并发问题都会被大家所诟病,正是因为全局解释锁的存在,导致其不能够真正的做到并发的执行。所以,我们就需要ipyparallel的存在来帮助我们处理并发计算的问题了。
    • ipyparallel中,可以利用多个engine同时运行一个任务来加快处理的速度。集群被抽象为view,包括direct_viewbalanced_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 in line.split

相关文章:

  • 2021-10-22
  • 2021-12-24
  • 2021-05-04
  • 2021-06-23
猜你喜欢
  • 2021-06-16
  • 2021-06-11
  • 2021-07-31
  • 2021-10-13
  • 2021-09-19
  • 2022-12-23
  • 2021-05-28
相关资源
相似解决方案