python分布式计算dispy的使用详解


Posted in Python onDecember 22, 2019

dispy,是用asyncoro实现的分布式并行计算框架。

框架也是非常精简,只有4个组件,在其源码文件夹下可以找到:

dispy.py (client) provides two ways of creating “clusters”: JobCluster when only one instance of dispy may run and SharedJobCluster when multiple instances may run (in separate processes). If JobCluster is used, the scheduler contained within dispy.py will distribute jobs on the server nodes; if SharedJobCluster is used, a separate scheduler (dispyscheduler) must be running.

dispynode.py executes jobs on behalf of dispy. dispynode must be running on each of the (server) nodes that form the cluster.

dispyscheduler.py is needed only when SharedJobCluster is used; this provides a scheduler that can be shared by multiple dispy users.

dispynetrelay.py is needed when nodes are located across different networks; this relays information about nodes on a network to the scheduler. If all the nodes are on same network, there is no need for dispynetrelay - the scheduler and nodes automatically discover each other.

一般情况下,使用dispy和dispynode就已经足够解决问题了。

简单使用:

服务器端:

在服务器端启动dispy,监听并接收所有发来的计算任务,完成计算后将结果返回给客户端。

打开python_home/Scripts文件夹,在安装dispy后会有上面说到的4个dispy组件,以py文件形式存在。当然你也可以在dispy的源码文件夹里面找到对于的dispynode.py文件,然后执行

python dispynode.py -c 2 -i 192.168.138.128 -p 51348 -s secret --clean

python dispynode.py -c 2 -i 192.168.8.143 -p 51348 -s secret --clean

这里192.168.138.128和192.168.8.143是执行计算节点的ip(对服务器来说相当于localhost),这里我启用了两个节点,每个节点使用2个cpu资源,其中有一个节点是在虚拟机,一个是本地机器。

-s secret是通信密码,客户端和服务器连接需要密码,密码随意。

--clean表示每次启动服务都删除上次的启动信息,如果不删除,可能会出现pid占用的错误。

客户端:

在客户端需要注意的是,发送到计算节点函数所引用的模块,不能在py文件的顶层导入,而需要在函数内导入。

对于需要导入自定义模块,比较麻烦一点,需要先实例化函数,才能在计算节点的函数中使用。

# 这些在顶层导入的模块只能是这个py文件用
import time
import socket
import numpy
import datetime

# 这个是自定义函数,要在本模块中先实例化才能在计算节点函数中调用使用,
# 而本模块的其他地方可以直接调用使用
from my_package.my_model import get_time 

# 实例化自定义的函数,注意后面是没有括号的,否则就是直接调用得到返回值了
now = get_time.now

# 计算函数,dispy将这个函数和参数一并发送到服务器节点
# 如果函数有多个参数,需要包装程tuple格式
def compute(args):
 n,array=args # 如果函数有多个参数,需要包装程tuple格式
 # 看到没,计算需要的模块是在函数内导入的
 import time, socket
 time.sleep(3)
 host = socket.gethostname()
 # 这个py文件中自定义函数,可以直接引用
 total= my_sum(array)
 # 这个now是在其他模块中自定义的函数,需要在顶层先实例化才能引用
 now_time=now()
 return (host, n, total,now_time)

def sum(array):
 # 自定义函数,需要的模块同样需要在函数内导入
 import numpy as np
 return np.sum(array)

def loadData():
 # 自定义函数,生成测试数据
 import numpy as np
 data = np.random.rand(20,20)
 data = [line for line in data]
 return data



if __name__ == '__main__':
 import dispy, random
 # 定义两个计算节点
 nodes = ['192.168.8.143', '192.168.138.128']
 # 启动计算集群,和服务器通信,通信密钥是'secret'
 # depends 为依赖函数
 cluster = dispy.JobCluster(compute,nodes=nodes,
      secret='secret',depends=[sum,now])
 jobs = []

 datas = loadData()
 for n in range(len(datas)):
  # 提交任务
  job = cluster.submit((n,datas[n]))
  job.id = n
  jobs.append(job)
 # print(datetime.datetime.now())
 # cluster.wait() # 等待所有任务完成后才接着往下执行
 # print(datetime.datetime.now())
 for job in jobs:
  host, n, total,t = job()
  print('%s executed job %s at %s with %s total=%.2f t=%s' 
    % (host, job.id, job.start_time, n,total,t))
  # other fields of 'job' that may be useful:
  # print job.stdout, job.stderr, job.exception, 
  # job.ip_addr, job.start_time, job.end_time
 # 显示集群计算状态
 cluster.stats()

以上这篇python分布式计算dispy的使用详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Django中针对基于类的视图添加csrf_exempt实例代码
Feb 11 Python
Python2.7 实现引入自己写的类方法
Apr 29 Python
redis之django-redis的简单缓存使用
Jun 07 Python
Python中文件的写入读取以及附加文字方法
Jan 23 Python
Python字典常见操作实例小结【定义、添加、删除、遍历】
Oct 25 Python
Tensorflow 1.0之后模型文件、权重数值的读取方式
Feb 12 Python
Python 实现Image和Ndarray互相转换
Feb 19 Python
使用Pycharm分段执行代码
Apr 15 Python
Ubuntu中配置TensorFlow使用环境的方法
Apr 21 Python
python3+opencv 使用灰度直方图来判断图片的亮暗操作
Jun 02 Python
python数据类型强制转换实例详解
Jun 22 Python
PyCharm 2020.2 安装详细教程
Sep 25 Python
使用python实现哈希表、字典、集合操作
Dec 22 #Python
浅析Python数字类型和字符串类型的内置方法
Dec 22 #Python
Python利用多线程同步锁实现多窗口订票系统(推荐)
Dec 22 #Python
python使用正则来处理各种匹配问题
Dec 22 #Python
Python中base64与xml取值结合问题
Dec 22 #Python
python操作cfg配置文件方式
Dec 22 #Python
python实现局域网内实时通信代码
Dec 22 #Python
You might like
刚才在简化php的库,结果发现很多东西
2006/12/31 PHP
PHP下判断网址是否有效的代码
2011/10/08 PHP
php中curl使用指南
2015/02/05 PHP
PHP结合jQuery实现找回密码
2015/07/22 PHP
php短信接口代码
2016/05/13 PHP
php上传excel表格并获取数据
2017/04/27 PHP
php数据序列化测试实例详解
2017/08/12 PHP
关于Curl在Swoole协程中的解决方案详析
2019/09/12 PHP
greybox——不开新窗口看新的网页
2007/02/20 Javascript
基于PHP+Jquery制作的可编辑的表格的代码
2011/04/10 Javascript
flash遮住div问题的正确解决方法
2014/02/27 Javascript
jQuery控制frames及frame页面JS的方法
2016/03/08 Javascript
js滚动条平滑移动示例代码
2016/03/29 Javascript
JS脚本实现动态给标签控件添加事件的方法
2016/06/02 Javascript
详细谈谈AngularJS的子级作用域问题
2016/09/05 Javascript
再谈javascript常见错误及解决方法
2016/09/16 Javascript
js实现倒计时器自定义时间和暂停
2019/02/25 Javascript
Javascript执行流程细节原理解析
2020/05/14 Javascript
[39:52]2018DOTA2亚洲邀请赛 4.3 突围赛 EG vs Newbee 第一场
2018/04/04 DOTA
Python中time模块与datetime模块在使用中的不同之处
2015/11/24 Python
Python 爬虫多线程详解及实例代码
2016/10/08 Python
Python实现利用最大公约数求三个正整数的最小公倍数示例
2017/09/30 Python
hmac模块生成加入了密钥的消息摘要详解
2018/01/11 Python
python3.6+selenium实现操作Frame中的页面元素
2019/07/16 Python
python面向对象之类属性和类方法案例分析
2019/12/30 Python
python 三种方法实现对Excel表格的读写
2020/11/19 Python
美国老牌主机服务商:iPage
2016/07/22 全球购物
PAUL HEWITT手表美国站:德国北部时尚生活配饰品牌,船锚元素
2017/11/18 全球购物
销售类个人求职信范文
2013/09/25 职场文书
国家励志奖学金获奖感言
2014/01/09 职场文书
出国考察邀请函
2014/01/21 职场文书
知识竞赛主持词
2014/03/26 职场文书
公司股份合作协议书
2014/12/07 职场文书
医院科室评语
2015/01/04 职场文书
2015年信息宣传工作总结
2015/05/26 职场文书
导游词之井冈山
2019/11/20 职场文书