async def submit(): gang = await tsk.ctx().wait_for_workers(2) X = 3.1 n = 10 pr = tsk.task(lambda: tsk.task(lambda: X)) for i in range(n): pr = pr.then(lambda x: x + 1) async def asum(x): return sum(x) pr = tsk.when_all([pr, tsk.task(lambda: X, to=gang[1])]).then(asum) print("answer is", await pr)
def fib(index): # If the problem is too small, just do it in serial... if index < 25: # async(f) tells taskloaf to run the parameterless function f # at some point in the future. The return value from async is a # future. You can think of a future as a box around a value that # will eventually be computed, but has not necessarily been computed # yet. Futures are eventually "filled" after the task has run. return tl.async(lambda: fib_serial(index)); else: # "when_all" tells taskloaf to return a new future that will be filled # with the values from the futures that are passed as arguments # after all those futures have been filled. # "when_all" is short for "when all of these have been computed" return tl.when_all( fib(index - 1), fib(index - 2) # "future.then" tells taskloaf to run the provided function on the # values inside the future after it has been filled ).then(lambda x, y: x + y)
def fib(index): if index < 3: return tl.ready(1) else: return tl.when_all(fib(index - 1), fib(index - 2)).then(add)
async def submit(w): n = await tsk.task(w, lambda w: 10, to = 1) pr = tsk.when_all( [tsk.task(w, lambda w, i = i: i, to = i % 2) for i in range(n)] ).then(lambda w, x: sum(x)) return (await pr)