Is it possible to pass a provisional result from a function other than the "computation" function? I tried to modify the provisional result example as described below, and all of the jobs are terminated.
def compute(n, threshold):
import random, time, socket
from returnResult import myfn
name = socket.gethostname()
for i in xrange(0, n):
r = random.uniform(0, 1)
#if r <= threshold:
# possible result
#dispy_provisional_result((name, r))
myfn(name, r)
time.sleep(0.1)
# final result
return None
def job_callback(job):
print job.id, job.status # check job status
if job.status == dispy.DispyJob.ProvisionalResult:
if job.result[1] < 0.005:
# acceptable result; terminate jobs
print '%s computed: %s' % (job.result[0], job.result[1])
# global jobs, cluster
for j in jobs:
if j.status in [dispy.DispyJob.Created, dispy.DispyJob.Running,
dispy.DispyJob.ProvisionalResult]:
cluster.cancel(j)
if name == 'main':
cluster = dispy.JobCluster(compute, callback=job_callback)
jobs = []
for n in xrange(4):
job = cluster.submit(random.randint(50,100), 0.2)
if job is None:
print 'creating job %s failed!' % n
continue
job.id = n
jobs.append(job)
cluster.wait()
cluster.stats()
cluster.close()
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Is it possible to pass a provisional result from a function other than the "computation" function? I tried to modify the provisional result example as described below, and all of the jobs are terminated.
In a file called returnResult.py, I put:
~~~~~~~~~~~~~~~~~~~~~~~
def myfn (name, r):
dispy_provisional_result((name, r))
return None
~~~~~~~~~~~~~~~~~~~~
Then I tried to run:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~``
import random, dispy
def compute(n, threshold):
import random, time, socket
from returnResult import myfn
name = socket.gethostname()
for i in xrange(0, n):
r = random.uniform(0, 1)
#if r <= threshold:
# possible result
#dispy_provisional_result((name, r))
myfn(name, r)
time.sleep(0.1)
# final result
return None
def job_callback(job):
print job.id, job.status # check job status
if job.status == dispy.DispyJob.ProvisionalResult:
if job.result[1] < 0.005:
# acceptable result; terminate jobs
print '%s computed: %s' % (job.result[0], job.result[1])
# global jobs, cluster
for j in jobs:
if j.status in [dispy.DispyJob.Created, dispy.DispyJob.Running,
dispy.DispyJob.ProvisionalResult]:
cluster.cancel(j)
if name == 'main':
cluster = dispy.JobCluster(compute, callback=job_callback)
jobs = []
for n in xrange(4):
job = cluster.submit(random.randint(50,100), 0.2)
if job is None:
print 'creating job %s failed!' % n
continue
job.id = n
jobs.append(job)
cluster.wait()
cluster.stats()
cluster.close()
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Calling dispy_provisional_result from a function that has been called from computation should work.
However, function 'myfn' should be distributed (otherwise 'compute' calls a function that doesn't exist so it fails); i.e., create cluster with
cluster = dispy.JobCluster(compute, depends=[myfn], callback=job_callback)
That works, thanks! And sorry for posting to the wrong board....