我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用tornado.gen.WaitIterator()。
def test_iterator(self): futures = [Future(), Future(), Future(), Future()] self.finish_coroutines(0, futures) g = gen.WaitIterator(*futures) i = 0 while not g.done(): try: r = yield g.next() except ZeroDivisionError: self.assertIs(g.current_future, futures[0], 'exception future invalid') else: if i == 0: self.assertEqual(r, 24, 'iterator value incorrect') self.assertEqual(g.current_index, 2, 'wrong index') elif i == 2: self.assertEqual(r, 42, 'iterator value incorrect') self.assertEqual(g.current_index, 1, 'wrong index') elif i == 3: self.assertEqual(r, 84, 'iterator value incorrect') self.assertEqual(g.current_index, 3, 'wrong index') i += 1
def test_empty_iterator(self): g = gen.WaitIterator() self.assertTrue(g.done(), 'empty generator iterated') with self.assertRaises(ValueError): g = gen.WaitIterator(False, bar=False) self.assertEqual(g.current_index, None, "bad nil current index") self.assertEqual(g.current_future, None, "bad nil current future")
def test_iterator_async_await(self): # Recreate the previous test with py35 syntax. It's a little clunky # because of the way the previous test handles an exception on # a single iteration. futures = [Future(), Future(), Future(), Future()] self.finish_coroutines(0, futures) self.finished = False namespace = exec_test(globals(), locals(), """ async def f(): i = 0 g = gen.WaitIterator(*futures) try: async for r in g: if i == 0: self.assertEqual(r, 24, 'iterator value incorrect') self.assertEqual(g.current_index, 2, 'wrong index') else: raise Exception("expected exception on iteration 1") i += 1 except ZeroDivisionError: i += 1 async for r in g: if i == 2: self.assertEqual(r, 42, 'iterator value incorrect') self.assertEqual(g.current_index, 1, 'wrong index') elif i == 3: self.assertEqual(r, 84, 'iterator value incorrect') self.assertEqual(g.current_index, 3, 'wrong index') else: raise Exception("didn't expect iteration %d" % i) i += 1 self.finished = True """) yield namespace['f']() self.assertTrue(self.finished)
def test_no_ref(self): # In this usage, there is no direct hard reference to the # WaitIterator itself, only the Future it returns. Since # WaitIterator uses weak references internally to improve GC # performance, this used to cause problems. yield gen.with_timeout(datetime.timedelta(seconds=0.1), gen.WaitIterator(gen.sleep(0)).next())
def judge_and_alert(instance): def _get_query_datas(): # ??????????ES???? alert_items = get_alert_items() _query_constructor = partial( get_alert_type_strategy_and_query_body, hostname=instance) return map(_query_constructor, alert_items) querys = _get_query_datas() strategys = {q['alertname']: q['strategy'] for q in querys} # alertname?key???es??????value workdic = {q['alertname']: send_es_query(q['query_body']) for q in querys} # ?????????ES???? waiter = gen.WaitIterator(**workdic) while not waiter.done(): try: es_ret = yield waiter.next() except Exception as e: logging.error(e, exc_info=True) else: alertname = waiter.current_index # ?????es???? alert_content = yield judge_result_and_make_alert_msg(alertname, strategys[alertname], json.loads(es_ret)) # ??????????? if alert_content.get('body'): receive_group = strategys[alertname]['contact_group'] alert_level = strategys[alertname]['level'] alerting = Alert(receive_group, alert_level, alert_content) # ??????? yield alerting.send()
def test_already_done(self): f1 = Future() f2 = Future() f3 = Future() f1.set_result(24) f2.set_result(42) f3.set_result(84) g = gen.WaitIterator(f1, f2, f3) i = 0 while not g.done(): r = yield g.next() # Order is not guaranteed, but the current implementation # preserves ordering of already-done Futures. if i == 0: self.assertEqual(g.current_index, 0) self.assertIs(g.current_future, f1) self.assertEqual(r, 24) elif i == 1: self.assertEqual(g.current_index, 1) self.assertIs(g.current_future, f2) self.assertEqual(r, 42) elif i == 2: self.assertEqual(g.current_index, 2) self.assertIs(g.current_future, f3) self.assertEqual(r, 84) i += 1 self.assertEqual(g.current_index, None, "bad nil current index") self.assertEqual(g.current_future, None, "bad nil current future") dg = gen.WaitIterator(f1=f1, f2=f2) while not dg.done(): dr = yield dg.next() if dg.current_index == "f1": self.assertTrue(dg.current_future == f1 and dr == 24, "WaitIterator dict status incorrect") elif dg.current_index == "f2": self.assertTrue(dg.current_future == f2 and dr == 42, "WaitIterator dict status incorrect") else: self.fail("got bad WaitIterator index {}".format( dg.current_index)) i += 1 self.assertEqual(dg.current_index, None, "bad nil current index") self.assertEqual(dg.current_future, None, "bad nil current future")