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

Dispy是一个基于Asyncoro的轻量级分布式并行计算框架,适用于任务分解型应用。该框架由四个主要组件构成,包括dispy.py、dispynode.py、dispyscheduler.py和dispynetrelay.py。通过简单的API,用户可以在多个节点上运行Python函数或外部程序,并收集结果。
摘要由CSDN通过智能技术生成


dispy:asyncoro实现的分布式并行计算框架。一个对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就已经足够解决问题了

  1. 服务端:

dispynode是服务端组件,它不需要写代码,只是使用参数运行为一个守护进程就OK了,比如:

dispynode.py -c 2 -i 192.168.0.10 -p 51348 -s secret

这个实例会使用2个cpu核心,绑定192.168.0.10:51348地址提供服务,secret是消息加密的共享密钥,更多参数参见http://dispy.sourceforge.net/dispynode.html

  1. 客户端:

简单例子:

#!/usr/bin/env python
def compute(n):
    import time, socket
    time.sleep(n)
    host = socket.gethostname()
    return (host, n)

if __name__ == '__main__':
    import dispy, random
    cluster = dispy.JobCluster(compute,nodes=['192.168.0.10', '192.168.3.11'])
    jobs = []
    for n in range(20):
        job = cluster.submit(random.randint(5,20))
        job.id = n
        jobs.append(job)
    # cluster.wait()
    for job in jobs:
        host, n = job()
        print '%s executed job %s at %s with %s' % (host, job.id, job.start_time, n)
        # 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()

JobCluster也可以使用回调来处理结果:

dispy.JobCluster(compute,nodes=['192.168.0.10', '192.168.3.11'],callback=callback)

除了python函数,也可以是调用服务端的程序,比如:

cluster = dispy.JobCluster('/some/program', nodes=['192.168.0.10'])

也可以不写任何代码,而作为一个命令工具来使用:

dispy.py -f /some/file1 -f file2 -a "arg11 arg12" -a "arg21 arg22" -a "arg3" /some/program

详细文档参见 http://dispy.sourceforge.net/index.html

如果嫌celery稍重的话,可以试试dispy


评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值