In [1]:
import os, sys
sys.path.append(os.path.abspath('../../main/python'))

In [2]:
import math
import time

import thalesians.tsa.evaluation as evaluation

In [3]:
def fact(x):
    time.sleep(10)
    return math.factorial(x)

In [4]:
current_thread_evaluator = evaluation.CurrentThreadEvaluator()

In [5]:
status = evaluation.evaluate(fact, args=[10], evaluator=current_thread_evaluator)
print(status)


Status(ready=True, work=Work(work_id="1e42bd16f9594d5baf8ad0eb32ef8d0b", func=<function fact at 0x00000196E4BEDD08>, args=[10], kwargs={}, call_count=1, repeat_count=1, info=None), result=Result(work_id="1e42bd16f9594d5baf8ad0eb32ef8d0b", func=<function fact at 0x00000196E4BEDD08>, args=[10], kwargs={}, call_count=1, repeat_count=1, info=None, evaluation_id="0d9fc60c2c9d413bb2b5570b4b42e931", result=3628800, exception=None, start_datetime=2019-04-21 11:59:11.034994, seconds_per_call=[10.000247200000002], hostname="PB-MP", pid=2004, thread_id=2004))

For the following to work, you need to run (for example)

ipcluster start --profile=mycluster --n=8

ipcluster is usually located in the Scripts directory of your Python distribution. If it is missing, you need to install the ipyparallel package, either using the Anaconda Navigator, or pip install ipyparallel, or conda install ipyparallel.

Also note that, for the following to work, this library must me on ipcluster's PYTHONPATH. Thus, for example, you would need to

set PYTHONPATH=%PYTHONPATH%;C:\Users\Paul\Documents\dev\tsa\src\main\python

(correct the above for your operating system and directory structure) before launching ipcluster with the above command.


In [6]:
ipp_evaluator = evaluation.IPyParallelEvaluator()

In [7]:
status1 = evaluation.evaluate(fact, args=[10], evaluator=ipp_evaluator)
print(status1)


Status(ready=False, work=Work(work_id="fb1e7cd966854f3a8c252dbc925d42e2", func=<function fact at 0x00000196E4BEDD08>, args=[10], kwargs={}, call_count=1, repeat_count=1, info=None), result=None)

In [8]:
status2 = evaluation.evaluate(fact, args=[8], evaluator=ipp_evaluator)
print(status2)


Status(ready=False, work=Work(work_id="9a42478d6a41459fa84610761e68bf0a", func=<function fact at 0x00000196E4BEDD08>, args=[8], kwargs={}, call_count=1, repeat_count=1, info=None), result=None)

In [9]:
def my_callback(status):
    print('The status is now ready:', status)
status3 = evaluation.evaluate(fact, args=[8], evaluator=ipp_evaluator)
status3.add_callback(my_callback)


The status is now ready: Status(ready=True, work=Work(work_id="e082308dd2754b7199d05dfd41def3ed", func=<function fact at 0x00000196E4BEDD08>, args=[8], kwargs={}, call_count=1, repeat_count=1, info=None), result=Result(work_id="e082308dd2754b7199d05dfd41def3ed", func=<function fact at 0x00000196E5FB9510>, args=[8], kwargs={}, call_count=1, repeat_count=1, info=None, evaluation_id="ba1a8279a65544ac9f258befb786b7c8", result=40320, exception=None, start_datetime=2019-04-21 11:59:36.525799, seconds_per_call=[10.000202999999999], hostname="PB-MP", pid=2980, thread_id=2980))

In [10]:
status1


Out[10]:
Status(ready=True, work=Work(work_id="fb1e7cd966854f3a8c252dbc925d42e2", func=<function fact at 0x00000196E4BEDD08>, args=[10], kwargs={}, call_count=1, repeat_count=1, info=None), result=Result(work_id="fb1e7cd966854f3a8c252dbc925d42e2", func=<function fact at 0x00000196E49A0F28>, args=[10], kwargs={}, call_count=1, repeat_count=1, info=None, evaluation_id="fd30d4b7ae5f408caa9f8a3ade0dacdb", result=3628800, exception=None, start_datetime=2019-04-21 11:59:32.738832, seconds_per_call=[9.9999957], hostname="PB-MP", pid=19528, thread_id=19528))

In [11]:
status2


Out[11]:
Status(ready=True, work=Work(work_id="9a42478d6a41459fa84610761e68bf0a", func=<function fact at 0x00000196E4BEDD08>, args=[8], kwargs={}, call_count=1, repeat_count=1, info=None), result=Result(work_id="9a42478d6a41459fa84610761e68bf0a", func=<function fact at 0x00000196E5FB9EA0>, args=[8], kwargs={}, call_count=1, repeat_count=1, info=None, evaluation_id="9cd924c5b1dd4fc3bf94903f3a8d70f1", result=40320, exception=None, start_datetime=2019-04-21 11:59:34.309822, seconds_per_call=[10.000351800000004], hostname="PB-MP", pid=23652, thread_id=23652))

In [12]:
status3


Out[12]:
Status(ready=True, work=Work(work_id="e082308dd2754b7199d05dfd41def3ed", func=<function fact at 0x00000196E4BEDD08>, args=[8], kwargs={}, call_count=1, repeat_count=1, info=None), result=Result(work_id="e082308dd2754b7199d05dfd41def3ed", func=<function fact at 0x00000196E5FB9510>, args=[8], kwargs={}, call_count=1, repeat_count=1, info=None, evaluation_id="ba1a8279a65544ac9f258befb786b7c8", result=40320, exception=None, start_datetime=2019-04-21 11:59:36.525799, seconds_per_call=[10.000202999999999], hostname="PB-MP", pid=2980, thread_id=2980))

In [13]:
statuses = []
for x in range(25):
    status = evaluation.evaluate(fact, args=[x], evaluator=ipp_evaluator)
    print(status)
    statuses.append(status)


Status(ready=False, work=Work(work_id="81cbf5f937cc4577a01c2c0a1b117655", func=<function fact at 0x00000196E4BEDD08>, args=[0], kwargs={}, call_count=1, repeat_count=1, info=None), result=None)
Status(ready=False, work=Work(work_id="255ebbae5556431eb4bdb0ed6e3b5ee4", func=<function fact at 0x00000196E4BEDD08>, args=[1], kwargs={}, call_count=1, repeat_count=1, info=None), result=None)
Status(ready=False, work=Work(work_id="a18e38bc9c404cc89f113b821e8d5f18", func=<function fact at 0x00000196E4BEDD08>, args=[2], kwargs={}, call_count=1, repeat_count=1, info=None), result=None)
Status(ready=False, work=Work(work_id="22b5409804af4cb7ab700dd71714ee19", func=<function fact at 0x00000196E4BEDD08>, args=[3], kwargs={}, call_count=1, repeat_count=1, info=None), result=None)
Status(ready=False, work=Work(work_id="9d4dc94010dc4c4494a907e14f85606d", func=<function fact at 0x00000196E4BEDD08>, args=[4], kwargs={}, call_count=1, repeat_count=1, info=None), result=None)
Status(ready=False, work=Work(work_id="f1955b0e3e7e47bf8f01f3f75d68b116", func=<function fact at 0x00000196E4BEDD08>, args=[5], kwargs={}, call_count=1, repeat_count=1, info=None), result=None)
Status(ready=False, work=Work(work_id="7fb14af1b933497e8368291c0a92052d", func=<function fact at 0x00000196E4BEDD08>, args=[6], kwargs={}, call_count=1, repeat_count=1, info=None), result=None)
Status(ready=False, work=Work(work_id="b9ba458d60014145b0138366beb9b364", func=<function fact at 0x00000196E4BEDD08>, args=[7], kwargs={}, call_count=1, repeat_count=1, info=None), result=None)
Status(ready=False, work=Work(work_id="61bf1b404c74470c92602a8b10a9587d", func=<function fact at 0x00000196E4BEDD08>, args=[8], kwargs={}, call_count=1, repeat_count=1, info=None), result=None)
Status(ready=False, work=Work(work_id="e0888c702f324731a8ee051b72e714dd", func=<function fact at 0x00000196E4BEDD08>, args=[9], kwargs={}, call_count=1, repeat_count=1, info=None), result=None)
Status(ready=False, work=Work(work_id="492bdfed977f4252afc48d772eb212b2", func=<function fact at 0x00000196E4BEDD08>, args=[10], kwargs={}, call_count=1, repeat_count=1, info=None), result=None)
Status(ready=False, work=Work(work_id="a475e928e2554bc4b051024d483c2cf9", func=<function fact at 0x00000196E4BEDD08>, args=[11], kwargs={}, call_count=1, repeat_count=1, info=None), result=None)
Status(ready=False, work=Work(work_id="68c5aa40784d4809bd28a2cb8b5ecdfb", func=<function fact at 0x00000196E4BEDD08>, args=[12], kwargs={}, call_count=1, repeat_count=1, info=None), result=None)
Status(ready=False, work=Work(work_id="6a20e5346f624c1cbce7d106bf05060f", func=<function fact at 0x00000196E4BEDD08>, args=[13], kwargs={}, call_count=1, repeat_count=1, info=None), result=None)
Status(ready=False, work=Work(work_id="d9952d984c6a46a79eb9155a00f6067d", func=<function fact at 0x00000196E4BEDD08>, args=[14], kwargs={}, call_count=1, repeat_count=1, info=None), result=None)
Status(ready=False, work=Work(work_id="16e6df3f254f4ec8b1c3d6c6a9349a97", func=<function fact at 0x00000196E4BEDD08>, args=[15], kwargs={}, call_count=1, repeat_count=1, info=None), result=None)
Status(ready=False, work=Work(work_id="86e9c9efee5f4b3eb065be479afad578", func=<function fact at 0x00000196E4BEDD08>, args=[16], kwargs={}, call_count=1, repeat_count=1, info=None), result=None)
Status(ready=False, work=Work(work_id="85e7e72cba1a404599d6978fc4bfd50f", func=<function fact at 0x00000196E4BEDD08>, args=[17], kwargs={}, call_count=1, repeat_count=1, info=None), result=None)
Status(ready=False, work=Work(work_id="d3dc3f66554f438ebd97680f926c7ab1", func=<function fact at 0x00000196E4BEDD08>, args=[18], kwargs={}, call_count=1, repeat_count=1, info=None), result=None)
Status(ready=False, work=Work(work_id="2871c01f934643719bf4932ce4c8858a", func=<function fact at 0x00000196E4BEDD08>, args=[19], kwargs={}, call_count=1, repeat_count=1, info=None), result=None)
Status(ready=False, work=Work(work_id="2b9cec1a54ca48ba9eaec41f4401981b", func=<function fact at 0x00000196E4BEDD08>, args=[20], kwargs={}, call_count=1, repeat_count=1, info=None), result=None)
Status(ready=False, work=Work(work_id="8bef4cd2e74346e1bb085a93e8143ee5", func=<function fact at 0x00000196E4BEDD08>, args=[21], kwargs={}, call_count=1, repeat_count=1, info=None), result=None)
Status(ready=False, work=Work(work_id="41ebde3424f241709c370c36e4993725", func=<function fact at 0x00000196E4BEDD08>, args=[22], kwargs={}, call_count=1, repeat_count=1, info=None), result=None)
Status(ready=False, work=Work(work_id="fa6453f6cfcb41b98cb12eb25683cce1", func=<function fact at 0x00000196E4BEDD08>, args=[23], kwargs={}, call_count=1, repeat_count=1, info=None), result=None)
Status(ready=False, work=Work(work_id="04bd46a136b54e3ca8597b518c57e080", func=<function fact at 0x00000196E4BEDD08>, args=[24], kwargs={}, call_count=1, repeat_count=1, info=None), result=None)

In [14]:
statuses[24]


Out[14]:
Status(ready=False, work=Work(work_id="04bd46a136b54e3ca8597b518c57e080", func=<function fact at 0x00000196E4BEDD08>, args=[24], kwargs={}, call_count=1, repeat_count=1, info=None), result=None)

In [15]:
statuses[24]


Out[15]:
Status(ready=True, work=Work(work_id="04bd46a136b54e3ca8597b518c57e080", func=<function fact at 0x00000196E4BEDD08>, args=[24], kwargs={}, call_count=1, repeat_count=1, info=None), result=Result(work_id="04bd46a136b54e3ca8597b518c57e080", func=<function fact at 0x00000196E4B95488>, args=[24], kwargs={}, call_count=1, repeat_count=1, info=None, evaluation_id="f3a45ac06f3346719806b59be4cf9fe1", result=620448401733239439360000, exception=None, start_datetime=2019-04-21 12:00:33.056383, seconds_per_call=[10.000303900000006], hostname="PB-MP", pid=18940, thread_id=18940))