首页 > 解决方案 > python dispy 无法将我的函数分布在节点上

问题描述

我是 python dispy 的新手,我正在尝试执行计算测试函数以在我的集群节点上进行性能测量。我在我的客户上写了这个:

from time import monotonic
from hashlib import sha256

import socket

# Mine local test
def mine(message, difficulty=1):
    assert difficulty >= 1
    prefix = '1' * difficulty
    hosts = []
    for i in range(10000000):
        digest = sha256(str(hash(message+ str(i))).encode('utf-8'))
        if digest.hexdigest().startswith(prefix):
            print ("after " + str(i) + " iterations found nonce: "+ digest.hexdigest())
        hosts.append(socket.gethostname())
    return hosts


if __name__ == '__main__':
    import dispy, random, socket
    # fetch the IP address of the client
    start_t = monotonic()
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    s.connect(("8.8.8.8", 80)) # doesn't matter if 8.8.8.8 can't be reached
    cluster = dispy.JobCluster(mine,ip_addr="172.16.1.20", nodes="172.16.1.*")
    jobs = []

    job = cluster.submit("Test text",4)
    jobs.append(job)

    for job in jobs:
        print("Preparing for starting job ", job)
        host = job() # waits for job to finish and returns results
    stop_t = monotonic()
    print('executed job %s at %s' % (host, (stop_t-start_t)))
cluster.print_status()
cluster.close()

但是当我使用 python3 compute.py 运行时,我得到了

在 0.11133299399989482 处执行的作业无

我的函数无法执行并报告说使用单个节点而不是集群中的所有节点,有人可以帮助我使用 python dispy 使用所有集群资源运行我的函数吗?

非常感谢

标签: pythonpython-3.xdispy

解决方案


推荐阅读