def get_cls_and_args(cls, number): print('get_cls_and_args') assert cls.get_cls_ac() is cls assert (yield cls.get_cls_ac.asynq()) is cls assert cls.get_cls().value() is cls assert (yield cls.get_cls()) is cls result((cls, number)); return
def capybara(): val1 = 0 try: val1 += yield async_fn.asynq(1) except OverflowError: val1 += yield async_fn.asynq(4) result(val1)
def foo(num_yield): if num_yield == 0: result(0) return yield ExternalCacheBatchItem(mc._batch, "get", "test") yield foo.asynq(num_yield - 1)
def recursive_call_with_dirty(): global i if i > 0: result(i); return i += 1 recursive_call_with_dirty.dirty() yield recursive_call_with_dirty.asynq()
def _push_async(channel, value): yield None while True: if channel.push(value, False) is future_true: result(future_true) return yield None
def incr(): global counter counter += 1 print("Counter: %i" % counter) result(counter) return yield
def capybara(oid): second_oid = yield cached_fn.asynq(oid) new_oid = yield async_fn.asynq(second_oid) def process(): return new_oid - 2 result(process())
def async_fn_with_yield(should_yield): with Ctx(): if should_yield: ret = yield ExternalCacheBatchItem(mc._batch, 'get', 'test') else: ret = 0 result(ret) return
def capybara(qid, include_deleted): if include_deleted: value = yield PropertyObject.load.asynq(qid, include_deleted=False) else: value = yield PropertyObject.load.asynq(qid, include_deleted=True) result(value)
def _pull_async(channel): yield None while True: item = channel.pull(False) if item is not future_empty: result(item) return yield None
def sum_async(i, dump_progress=False): if i == 0: result(0) return if dump_progress and i % 100 == 0: print('... in sum_async(%s)' % i) result(i + (yield sum_async.asynq(i - 1, dump_progress))) return
def _call_pure(self, args, kwargs): try: result(self.cache[args]) return except KeyError: value = yield self.async_fn(*args) self.cache[args] = value result(value) return
def throw(raise_in_pause, raise_in_resume): with SimpleContext(): with ContextThatRaises(raise_in_pause, raise_in_resume): with SimpleContext(): # we need this to have a real dependency on an async task, otherwise # it executes the whole function inline and the real problem is never tested val = yield dependency.asynq() result(val) return
def capybara(qids, t): @asynq() def filter_fn(qid): result( (yield PropertyObject(qid).get_prop_with_get.asynq()) < t) qids = yield afilter.asynq(filter_fn, qids) system_a2a_ret = yield tuple(async_fn.asynq(qid) for qid in qids) result(system_a2a_ret)
def throw(expected_counter, must_throw): global counter print(" In throw, counter=%i (expected %i), must_throw=%s" % (counter, expected_counter, str(must_throw))) assert expected_counter == counter if must_throw: raise RuntimeError counter += 1 result(counter) return
def wrapped(*args): key = key_prefix + ':' + ':'.join(map(str, args)) value = yield self.get.asynq(key) if value is MISS: # possible enhancement: make it possible for the inner function to be async value = fn(*args) # there is a race condition here since somebody else could have set the key # while we were computing the value, but don't worry about that for now yield self.set.asynq(key, value) result(value); return
def wrapped(*args): key = key_prefix + ':' + ':'.join(map(str, args)) value = yield self.get.asynq(key) if value is MISS: # possible enhancement: make it possible for the inner function to be async value = fn(*args) # there is a race condition here since somebody else could have set the key # while we were computing the value, but don't worry about that for now yield self.set.asynq(key, value) result(value) return
def add_twice(a, b): global change_amount global expected_change_amount_base assert_eq(expected_change_amount_base, change_amount) with AsyncAddChanger(1): assert_eq(expected_change_amount_base + 1, change_amount) z = yield async_add(a, b) assert_eq(expected_change_amount_base + 1, change_amount) with AsyncAddChanger(1): q = yield async_add(a, b) assert_eq(expected_change_amount_base + 2, change_amount) assert_eq(expected_change_amount_base + 1, change_amount) assert_eq(expected_change_amount_base + 0, change_amount) result((yield async_add(z, q))) return
def test_async(): global counter try: print('In try block.') yield incr() result((yield incr())) # ; return assert False, "Must not reach this point!" except BaseException as e: print('In except block, e = ' + repr(e)) assert sync_incr() == 3 if isinstance(e, GeneratorExit): raise assert False, "Must not reach this point!" finally: print('In finally block.') assert sync_incr() == 4
def method(self, number): assert type(self) is MyClass cls, one = self.get_cls_and_args(number) assert cls is MyClass assert one == number cls, one = yield self.get_cls_and_args.asynq(number) assert cls is MyClass assert one == number one = yield self.static(number) assert one == number one = yield self.static_ac.asynq(number) assert one == number cls, proxied = yield self.async_proxy_classmethod.asynq(number) assert cls is MyClass assert proxied == number result(self); return
def async_scoped_value_helper(inner_val): @asynq() def nested(): assert_eq(v.get(), inner_val) yield DebugBatchItem() with v.override('c'): yield DebugBatchItem() # just so other function gets scheduled assert_eq(v.get(), 'c') yield DebugBatchItem() assert_eq(v.get(), 'a') yield DebugBatchItem() with v.override(inner_val): yield DebugBatchItem() assert_eq(v.get(), inner_val) result((yield nested.asynq())) return
def is_prop_with_is(self): result((yield cached_fn.asynq(self.poid))) return
def func(depth): if depth == 0: result((yield ExternalCacheBatchItem(mc._batch, 'get', 'test'))) return time.sleep(float(sleep_time) / SECOND) yield func.asynq(depth - 1), func.asynq(depth - 1)
def return_three_and_five(self): result((yield (self.return_three.asynq(), self.return_five.asynq()))) return
def async_fn(oid): result((yield cached_fn.asynq(oid))) return
def get_async(self): yield async_fn.asynq(1) result(2) return
def autogenerated(aid): result((yield async_fn.asynq(aid))) return
def get_prop_with_get(self): result((yield cached_fn.asynq(self.poid))) return
def async_classmethod(cls, poid): result((yield cls(poid).get_prop_with_get.asynq())) return
def async_method(self): result((yield cached_fn.asynq(self.poid))) return
def load(cls, poid, include_deleted=False): result((yield cls(poid).get_prop_with_get.asynq())) return