菜单 学习猿地 - LMONKEY

VIP

开通学习猿地VIP

尊享10项VIP特权 持续新增

知识通关挑战

打卡带练!告别无效练习

接私单赚外块

VIP优先接,累计金额超百万

学习猿地私房课免费学

大厂实战课仅对VIP开放

你的一对一导师

每月可免费咨询大牛30次

领取更多软件工程师实用特权

入驻
91
0

IPython并行计算工具

原创
05/13 14:22
阅读数 53139

 

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

发表评论

0/200
91 点赞
0 评论
收藏