我目前正在使用dispy执行10个随机数的阶乘计算,其中" 分发"各种节点的任务。 但是,如果其中一个计算是大数的阶乘,请说阶乘(100),那么如果该任务需要很长时间但是,dispy只在一个节点上运行。
如何确保dispy发生故障并将此任务分发给其他节点,以便它不会花费这么多时间?
这是我到目前为止提出的代码,其中计算了10个随机数的阶乘,第5个计算总是阶乘(100): -
# 'compute' is distributed to each node running 'dispynode'
def compute(n):
import time, socket
ans = 1
for i in range(1,n+1):
ans = ans * i
time.sleep(n)
host = socket.gethostname()
return (host, n,ans)
if __name__ == '__main__':
import dispy, random
cluster = dispy.JobCluster(compute)
jobs = []
for i in range(10):
# schedule execution of 'compute' on a node (running 'dispynode')
# with a parameter (random number in this case)
if(i==5):
job = cluster.submit(100)
else:
job = cluster.submit(random.randint(5,20))
job.id = i # optionally associate an ID to job (if needed later)
jobs.append(job)
# cluster.wait() # waits for all scheduled jobs to finish
for job in jobs:
host, n, ans = job() # waits for job to finish and returns results
print('%s executed job %s at %s with %s as input and %s as output' % (host, job.id, job.start_time, n,ans))
# 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.print_status()
答案 0 :(得分:0)
Dispy会在您定义任务时分发任务 - 它不会使任务更精细。
您可以先创建自己的逻辑来制作任务。对于阶乘来说,这可能很容易。但是我想知道在你的情况下性能问题是否归因于这一行:
time.sleep(n)
对于阶乘(100),你为什么要睡100秒?