【问题标题】:What does asyncio.test_utils.run_briefly exactly do?asyncio.test_utils.run_briefly 究竟做了什么?
【发布时间】:2018-05-23 09:22:10
【问题描述】:

据我所知,asyncio.test_utils 没有记录在案,因为它是供私人使用的(请参阅此issue)。

不过,我想知道asyncio.test_utils.run_briefly 的剂量。

例如,在这里,你能解释一下吗?

def test_gather_shield(self):
        child1 = asyncio.Future(loop=self.loop)
        child2 = asyncio.Future(loop=self.loop)
        inner1 = asyncio.shield(child1, loop=self.loop)
        inner2 = asyncio.shield(child2, loop=self.loop)
        parent = asyncio.gather(inner1, inner2, loop=self.loop)
        test_utils.run_briefly(self.loop)
        parent.cancel()
        # This should cancel inner1 and inner2 but bot child1 and child2.
        test_utils.run_briefly(self.loop)
        self.assertIsInstance(parent.exception(), asyncio.CancelledError)
        self.assertTrue(inner1.cancelled())
        self.assertTrue(inner2.cancelled())
        child1.set_result(1)
        child2.set_result(2)
        test_utils.run_briefly(self.loop)

【问题讨论】:

    标签: python python-3.6 python-asyncio


    【解决方案1】:

    帮助器进行单个事件循环迭代。 它使asyncio 有机会执行所有待处理的活动,例如loop.call_soon() 等。

    大致相当于loop.run_until_complete(asyncio.sleep(0))

    【讨论】:

      猜你喜欢
      • 2012-07-23
      • 2016-09-10
      • 2023-03-15
      • 2012-10-17
      • 2021-06-04
      • 2018-07-30
      • 2019-10-06
      • 2021-01-01
      • 2011-11-09
      相关资源
      最近更新 更多