def __await__(self): if taskloaf.ctx().name != self.ref.owner: self.ensure_future_exists() taskloaf.ctx().messenger.send(self.ref.owner, taskloaf.ctx().protocol.AWAIT, [self]) result_ref = yield from self._get_future().__await__() out = yield from result_ref.get().__await__() if isinstance(out, TaskExceptionCapture): raise out.e return out
def await_handler(args): req_addr = args[0] pr = args[1] async def await_wrapper(): result_ref = await pr._get_future() taskloaf.ctx().messenger.send(req_addr, taskloaf.ctx().protocol.SETRESULT, [pr, result_ref]) taskloaf.ctx().executor.run_work(await_wrapper)
def task_runner(pr, in_f, *in_args): async def task_wrapper(): f = await ensure_obj(in_f) args = [] for a in in_args: args.append(await ensure_obj(a)) try: result = await taskloaf.ctx().executor.wait_for_work(f, *args) # catches all exceptions except system-exiting exceptions that inherit # from BaseException except Exception as e: logger.exception("exception during task") result = TaskExceptionCapture(e) _unwrap_promise(pr, result) taskloaf.ctx().executor.run_work(task_wrapper)
def _unwrap_promise(pr, result): if isinstance(result, Promise): def unwrap_then(x): _unwrap_promise(pr, x) result.then(unwrap_then) else: result_ref = put(result) if pr.ref.owner == taskloaf.ctx().name: pr.set_result(result_ref) else: taskloaf.ctx().messenger.send( pr.ref.owner, taskloaf.ctx().protocol.SETRESULT, [pr, result_ref], )
def task(f, *args, to=None): ctx = taskloaf.ctx() if to is None: to = ctx.name out_pr = Promise(to) if to == ctx.name: task_runner(out_pr, f, *args) else: msg_objs = [out_pr, ensure_ref(f)] + [ensure_ref(a) for a in args] ctx.messenger.send(to, ctx.protocol.TASK, msg_objs) return out_pr
async def f(): import time start = time.time() while len(tsk.ctx().messenger.endpts.keys()) < 2: await asyncio.sleep(0) # print("HI") gang = list(tsk.ctx().messenger.endpts.keys()) end = time.time() print(end - start) print(end - start) print(end - start) print(end - start) print(gang) print(gang) print(gang) print(gang) print(gang) print(gang) print("LAUNCH") await tsk.task(lambda: print("hI"), to=gang[0]) print("DONE") return 123
async def f_wrapper(): if f is not None: result = await tsk.ctx().executor.wait_for_work(f) tsk.ctx().executor.stop = True f_wrapper.result = result
def _get_future(self): return taskloaf.ctx().promises[self.ref._id]
def ensure_future_exists(self): taskloaf.ctx().promises[self.ref._id] = asyncio.Future( loop=taskloaf.ctx().executor.ioloop)
def on_delete(_id): del taskloaf.ctx().promises[_id]
async def await_wrapper(): result_ref = await pr._get_future() taskloaf.ctx().messenger.send(req_addr, taskloaf.ctx().protocol.SETRESULT, [pr, result_ref])
def __init__(self, names=None): if names is None: names = tsk.ctx().get_all_names() self.names = names