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 相关文章推荐
python写入中英文字符串到文件的方法
May 06 Python
对命令行模式与python交互模式介绍
May 12 Python
Python3实现统计单词表中每个字母出现频率的方法示例
Jan 28 Python
pandas修改DataFrame列名的实现方法
Feb 22 Python
Python设置matplotlib.plot的坐标轴刻度间隔以及刻度范围
Jun 25 Python
如何通过python的fabric包完成代码上传部署
Jul 29 Python
Python3.6安装卸载、执行命令、执行py文件的方法详解
Feb 20 Python
python之生成多层json结构的实现
Feb 27 Python
Python threading.local代码实例及原理解析
Mar 16 Python
Python中格式化字符串的四种实现
May 26 Python
python飞机大战游戏实例讲解
Dec 04 Python
浅析Python中的随机采样和概率分布
Dec 06 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 出现乱码和Sessions验证问题的解决方法!
2008/12/06 PHP
IE6中使用position导致页面变形的解决方案(js代码)
2011/01/09 Javascript
下拉菜单点击实现连接跳转功能的js代码
2013/05/19 Javascript
jquery live()调用不存在的解决方法
2014/02/26 Javascript
对JavaScript中this指针的新理解分享
2015/01/31 Javascript
jQuery实现鼠标悬停显示提示信息窗口的方法
2015/04/30 Javascript
jQuery实现气球弹出框式的侧边导航菜单效果
2015/09/22 Javascript
js实现密码强度检测【附示例】
2016/03/30 Javascript
详解Jquery实现ready和bind事件
2016/04/14 Javascript
JavaScript中函数声明与函数表达式的区别详解
2016/08/18 Javascript
jQuery使用unlock.js插件实现滑动解锁
2017/04/04 jQuery
JavaScript 通过Ajax 动态加载CheckBox复选框
2017/08/31 Javascript
JavaScript类的继承方法小结【组合继承分析】
2018/07/11 Javascript
浅谈express.js框架中间件(middleware)
2019/04/07 Javascript
详解vue-cli3多页应用改造
2019/06/04 Javascript
使用apifm-wxapi快速开发小程序过程详解
2019/08/05 Javascript
JS实现使用POST方式发送请求
2019/08/30 Javascript
[03:37]2014DOTA2国际邀请赛 主赛事第一日胜者组TOPPLAY
2014/07/19 DOTA
python修改字典内key对应值的方法
2015/07/11 Python
解决python nohup linux 后台运行输出的问题
2018/05/11 Python
python+mysql实现教务管理系统
2019/02/20 Python
python实现微信机器人: 登录微信、消息接收、自动回复功能
2019/04/29 Python
python保存字典和读取字典的实例代码
2019/07/07 Python
基于pandas中expand的作用详解
2019/12/17 Python
Python可视化工具如何实现动态图表
2020/10/23 Python
英国第一摩托车和摩托车越野配件商店:GhostBikes
2019/03/10 全球购物
北京RT科技有限公司.net工程师面试题
2013/02/15 面试题
珍惜水资源建议书
2014/03/12 职场文书
综合实践活动总结
2014/05/05 职场文书
关于美容院的活动方案
2014/08/14 职场文书
运动会广播稿50字-100字
2014/10/11 职场文书
教师学习党的群众路线教育实践活动心得体会
2014/10/31 职场文书
高中班主任心得体会
2016/01/07 职场文书
毕业生的自我鉴定表范文
2019/05/16 职场文书
小学生节约用水倡议书
2019/08/12 职场文书
golang生成并解析JSON
2022/04/14 Golang