当前位置: 首页>>代码示例>>Python>>正文


Python Fiber.throw方法代码示例

本文整理汇总了Python中fibers.Fiber.throw方法的典型用法代码示例。如果您正苦于以下问题:Python Fiber.throw方法的具体用法?Python Fiber.throw怎么用?Python Fiber.throw使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在fibers.Fiber的用法示例。


在下文中一共展示了Fiber.throw方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_kill

# 需要导入模块: from fibers import Fiber [as 别名]
# 或者: from fibers.Fiber import throw [as 别名]
 def test_kill(self):
     def f():
         try:
             switch("ok")
             switch("fail")
         except Exception as e:
             return e
     g = Fiber(f)
     res = g.switch()
     self.assertEqual(res, "ok")
     res = g.throw(ValueError)
     self.assertTrue(isinstance(res, ValueError))
     self.assertFalse(g.is_alive())
开发者ID:pombredanne,项目名称:python-fibers,代码行数:15,代码来源:test_throw.py

示例2: test_class

# 需要导入模块: from fibers import Fiber [as 别名]
# 或者: from fibers.Fiber import throw [as 别名]
 def test_class(self):
     def f():
         try:
             switch("ok")
         except RuntimeError:
             switch("ok")
             return
         switch("fail")
     g = Fiber(f)
     res = g.switch()
     self.assertEqual(res, "ok")
     res = g.throw(RuntimeError)
     self.assertEqual(res, "ok")
开发者ID:pombredanne,项目名称:python-fibers,代码行数:15,代码来源:test_throw.py

示例3: test_val

# 需要导入模块: from fibers import Fiber [as 别名]
# 或者: from fibers.Fiber import throw [as 别名]
    def test_val(self):
        def f():
            try:
                switch("ok")
            except RuntimeError:
                val = sys.exc_info()[1]
                if str(val) == "ciao":
                    switch("ok")
                    return
            switch("fail")

        g = Fiber(f)
        res = g.switch()
        self.assertEqual(res, "ok")
        res = g.throw(RuntimeError("ciao"))
        self.assertEqual(res, "ok")

        g = Fiber(f)
        res = g.switch()
        self.assertEqual(res, "ok")
        res = g.throw(RuntimeError, "ciao")
        self.assertEqual(res, "ok")
开发者ID:pombredanne,项目名称:python-fibers,代码行数:24,代码来源:test_throw.py

示例4: test_throw_goes_to_original_parent

# 需要导入模块: from fibers import Fiber [as 别名]
# 或者: from fibers.Fiber import throw [as 别名]
    def test_throw_goes_to_original_parent(self):
        main = fibers.current()

        def f1():
            try:
                main.switch("f1 ready to catch")
            except IndexError:
                return "caught"
            else:
                return "normal exit"

        def f2():
            main.switch("from f2")

        g1 = Fiber(f1)
        g2 = Fiber(target=f2, parent=g1)
        self.assertRaises(IndexError, g2.throw, IndexError)
        self.assertFalse(g2.is_alive())
        self.assertTrue(g1.is_alive())    # g1 is skipped because it was not started

        g1 = Fiber(f1)
        g2 = Fiber(target=f2, parent=g1)
        res = g1.switch()
        self.assertEqual(res, "f1 ready to catch")
        res = g2.throw(IndexError)
        self.assertEqual(res, "caught")
        self.assertFalse(g2.is_alive())
        self.assertFalse(g1.is_alive())

        g1 = Fiber(f1)
        g2 = Fiber(target=f2, parent=g1)
        res = g1.switch()
        self.assertEqual(res, "f1 ready to catch")
        res = g2.switch()
        self.assertEqual(res, "from f2")
        res = g2.throw(IndexError)
        self.assertEqual(res, "caught")
        self.assertFalse(g2.is_alive())
        self.assertFalse(g1.is_alive())
开发者ID:Dmdv,项目名称:python-fibers,代码行数:41,代码来源:test_throw.py


注:本文中的fibers.Fiber.throw方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。