python分布式计算dispy的使用详解
作者:振裕 发布时间:2021-01-14 09:15:24
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()
来源:https://blog.csdn.net/suzyu12345/article/details/53909688
猜你喜欢
- 继上篇博客Python实现简易通讯录后,我就想写一个复杂点的学生信息管理系统,这次实现的功能有1.学生信息的录入管理;2.学生选课操作;3.
- 目录为什么要画局部放大图?程序逻辑程序实例总结这项功能的目的是为了方便使用opencv做图像标注工具。为什么要画局部放大图?在做图像数据标注
- 自己前端开发中常用到的一些技巧及问题解决方法,会常更新,希望对前端路上的朋友有帮助。1、文章标题列表中日期居右显示的方法(提供了两种方法,使
- 对于需要大量翻译的数据,人工翻译太慢,此时需要使用软件进行批量翻译。1.使用360的翻译def fanyi_word_cn(string):
- 本文实例讲述了Python基于Socket实现的简单聊天程序。分享给大家供大家参考,具体如下:需求:SCIENCE 和MOOD两个人软件专业
- 最近认识了一个做Python语音识别的朋友,聊天时候说到,未来五到十年,Python人工智能会在国内掀起一股狂潮,对各种应用的冲击,不下于淘
- 介绍Django是一个Web框架——一套用于帮助开发交互式网站的工具。Django能够响应网页请求,还能让我们更轻松地读写数据库、管理用户等
- 一.权限表mysql数据库中的3个权限表:user 、db、 host权限表的存取过程是:1)先从user表中的host、 user、 pa
- 一、标签语法由%}和 {% 来定义的,例如:{%tag%} {%endtag%},完整的标签有开始就有结束,如条件语句,有条件判断的开始,也
- 这是一个access较为豪华的包装范例,他调用了flash的流媒体控件,利用flash的交互与通信功能,借以达到了我们装扮软件的目的。fla
- 我在按照 Byte of python一步步的学习Python, 在学到‘解决方案'的时候,原文的实例 “backup_ver1.p
- 优点:·减少使用空间·使后面的背景层模糊或者渐隐出现让用户的焦点集中在当前层。什么时候使用(对话框等)模式窗口?·图片/视频灯箱效果·联系表
- 嵌套SELECT语句也叫子查询,形如:SELECT name FROM bbc WHERE region = (SELECT region
- 前言本篇和大家分享的是使用python简化对jar包操作命令,封装成简短关键字或词,达到操作简便的目的。最近在回顾和构思shell脚本工具,
- scatter绘画散点图代码如下:import matplotlib.pyplot as pltplt.scatter(x,y,
- 高考在即,笔者想为孩子以后能够快乐学习数学、学习编程找到一个比较合适的项目,经过一番比较发现github上的万星项目manim(https:
- Django功能强大不单在于他先进的编程理念,很多现有的功能模块更是可以直接拿来使用,比如这个牛掰的admin模块,可以作为一个很好的信息登
- 本文实例讲述了Python实现数通设备端口使用情况监控的方法。分享给大家供大家参考。具体如下:最近因工作需要,上面要求,每天需上报运维的几百
- 导语:除夕除夕,就是除去烦脑,迎接新的希望!在这里小编先祝大家除夕快乐,岁岁常欢笑,事事皆如意!正文:创建画布setup和draw是p5.j
- 原文地址:30 Days of Mootools 1.2 Tutorials - Day 14 - Periodical and Intro