本文整理汇总了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())
示例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")
示例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")
示例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())