def test_list_promise_if(): p1 = Promise() p2 = Promise() pd1 = listPromise([p1, p2]) pd2 = listPromise([p1]) pd3 = listPromise([]) assert p1.isPending assert p2.isPending assert pd1.isPending assert pd2.isPending assert pd3.isFulfilled p1.fulfill(5) assert p1.isFulfilled assert p2.isPending assert pd1.isPending assert pd2.isFulfilled p2.fulfill(10) assert p1.isFulfilled assert p2.isFulfilled assert pd1.isFulfilled assert pd2.isFulfilled assert_equals(5, p1.value) assert_equals(10, p2.value) assert_equals(5, pd1.value[0]) assert_equals(5, pd2.value[0]) assert_equals(10, pd1.value[1]) assert_equals([], pd3.value)
def wrapped(*args, **kwargs): # print "calling", f, "with", kwargs # key_values = kwargs.items() key_promise = list([(key, promisify(value)) for key, value in kwargs.items()]) # key_promise = [(key, promisify(value)) for key, value in key_values] arg_promises = list([promisify(value) for value in args]) kwarg_promises = list([promise for key, promise in key_promise]) promises = arg_promises + kwarg_promises for promise in promises: def echo_error(exc, promise=promise): print("error with ", promise, "exception is", exc) raise exc def echo(value, promise=promise): print("done with ", repr(promise), "value is", value) return value # promise.then(echo, echo_error) # print promises allarguments = aplus.listPromise(*promises) def call(_): kwargs_real = {key: promise.get() for key, promise in key_promise} args_real = list([promise.get() for promise in arg_promises]) return f(*args_real, **kwargs_real) return allarguments.then(call, _log_error("delayed decorator"))
def wrapped(*args, **kwargs): # print "calling", f, "with", kwargs # key_values = kwargs.items() key_promise = list([(key, promisify(value)) for key, value in kwargs.items()]) # key_promise = [(key, promisify(value)) for key, value in key_values] arg_promises = list([promisify(value) for value in args]) kwarg_promises = list([promise for key, promise in key_promise]) promises = arg_promises + kwarg_promises for promise in promises: def echo_error(exc, promise=promise): print("error with ", promise, "exception is", exc) # raise exc def echo(value, promise=promise): print("done with ", repr(promise), "value is", value) # promise.then(echo, echo_error) # print promises allarguments = aplus.listPromise(*promises) def call(_): kwargs_real = {key: promise.get() for key, promise in key_promise} args_real = list([promise.get() for promise in arg_promises]) return f(*args_real, **kwargs_real) def error(exc): if os.environ.get('VAEX_DEBUG', False): print("Error from delayed", exc) # import vaex # vaex.utils.print_stack_trace() raise exc return allarguments.then(call, error)
def wrapped(*args, **kwargs): # print "calling", f, "with", kwargs # key_values = kwargs.items() key_promise = list([(key, promisify(value)) for key, value in kwargs.items()]) # key_promise = [(key, promisify(value)) for key, value in key_values] arg_promises = list([promisify(value) for value in args]) kwarg_promises = list([promise for key, promise in key_promise]) promises = arg_promises + kwarg_promises for promise in promises: def echo_error(exc, promise=promise): print("error with ", promise, "exception is", exc) # raise exc def echo(value, promise=promise): print("done with ", repr(promise), "value is", value) # promise.then(echo, echo_error) # print promises allarguments = aplus.listPromise(*promises) def call(_): kwargs_real = {key: promise.get() for key, promise in key_promise} args_real = list([promise.get() for promise in arg_promises]) return f(*args_real, **kwargs_real) def error(exc): print("error", exc) raise exc return allarguments.then(call, error)
def _apply_all(self, name, *args, **kwargs): promises = [] print("calling %s (selection: %s)" % (name, self.is_masked)) selection_name = "default" selection = self.dataset.get_selection(name=selection_name) if selection: print(selection, selection.to_dict()) for dataset in self.dataset.datasets: dataset.set_selection(selection) #, selection_name=selection_name) subspace = dataset(*self.expressions, async=True) if self.is_masked: subspace = subspace.selected() print(subspace.get_selection(), dataset.get_selection("default"), subspace.is_masked, self.is_masked) import time t0 = time.time() def timit(o, dataset=dataset, t0=t0): print("took %s %f" % (dataset.server.hostname, time.time() - t0)) return o def error(e, dataset=dataset): print("issues with %s (%r)" % (dataset.server.hostname, e)) try: raise e except: logger.exception("error in error handler") #subspace.histogram(limits, size=size, weight=weight).then(timit, error) f = getattr(subspace, name) promise = f(*args, **kwargs).then(timit, error) promises.append(promise) return aplus.listPromise(promises)
def _apply_all(self, name, *args, **kwargs): promises = [] print("calling %s (selection: %s)" % (name, self.is_masked)) selection_name = "default" selection = self.dataset.get_selection(name=selection_name) if selection: print(selection, selection.to_dict()) for dataset in self.dataset.datasets: dataset.set_selection(selection) # , selection_name=selection_name) subspace = dataset(*self.expressions, delay=True) if self.is_masked: subspace = subspace.selected() print(subspace.get_selection(), dataset.get_selection("default"), subspace.is_masked, self.is_masked) import time t0 = time.time() def timit(o, dataset=dataset, t0=t0): print("took %s %f" % (dataset.server.hostname, time.time() - t0)) return o def error(e, dataset=dataset): print("issues with %s (%r)" % (dataset.server.hostname, e)) try: raise e except: logger.exception("error in error handler") # subspace.histogram(limits, size=size, weight=weight).then(timit, error) f = getattr(subspace, name) promise = f(*args, **kwargs).then(timit, error) promises.append(promise) return aplus.listPromise(promises)
def promisify(value): # TODO, support futures etc.. if isinstance(value, aplus.Promise): return value if isinstance(value, (list, tuple)): return aplus.listPromise(*list([promisify(k) for k in value])) else: return aplus.Promise.fulfilled(value)
def promisify(value): # TODO, support futures etc.. if isinstance(value, aplus.Promise): return value if isinstance(value, (list, tuple)): return aplus.listPromise(*list([promisify(k) for k in value])) if isinstance(value, dict): return aplus.dictPromise({k: promisify(v) for k, v in value.items()}) else: return aplus.Promise.fulfilled(value)
def __transaction(self, secret, account, on_success, async): def on_promise(seq_fee): tx_json = on_success(seq_fee) tx_blob = self.signer.sign(tx_json, secret, self.testnet) return self.submit_transaction(tx_blob, async=True) seq = self.get_sequence_number(account, async=True) fee = self.get_fee(async=True) p = listPromise([seq, fee]).then(on_promise) async = async if async else self. async
def __transaction(self, secret, account, on_success, async): def on_promise(seq_fee): tx_json = on_success(seq_fee) tx_blob = self.signer.sign(tx_json, secret, self.testnet) return self.submit_transaction(tx_blob, async=True) seq = self.get_sequence_number(account, async=True) fee = self.get_fee(async=True) p = listPromise([seq, fee]).then(on_promise) async = async if async else self.async
def test_list_promise_if(): p1 = Promise() p1.fulfill(5) p2 = Promise() p2.fulfill(10) pl = listPromise(p1, p2) assert p1.isFulfilled() assert p2.isFulfilled() assert pl.isFulfilled() assert_equals(5, p1.value) assert_equals(10, p2.value) assert_equals(5, pl.value[0]) assert_equals(10, pl.value[1])
def test_list_promise_when(): p1 = Promise() p2 = Promise() pl = listPromise(p1, p2) assert p1.isPending assert p2.isPending assert pl.isPending p1.fulfill(5) assert p1.isFulfilled assert p2.isPending assert pl.isPending p2.fulfill(10) assert p1.isFulfilled assert p2.isFulfilled assert pl.isFulfilled assert_equals(5, p1.value) assert_equals(10, p2.value) assert_equals(5, pl.value[0]) assert_equals(10, pl.value[1])
def _apply_all(self, name, *args, **kwargs): promises = [] logger.info("calling %s (args: %r, kwargs: %r)", name, args, kwargs) kwargs['delay'] = True for dataset in self.datasets: import time t0 = time.time() def timit(o, dataset=dataset, t0=t0): logger.info("took %s %f for %r" % (dataset.server.hostname, time.time() - t0, o)) return o def error(e, dataset=dataset): logger.error("issues with %s (%r)" % (dataset.server.hostname, e)) try: raise e except: logger.exception("error in error handler") f = getattr(dataset, name) promise = f(*args, **kwargs).then(timit, error) promises.append(promise) return aplus.listPromise(promises)