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


Python Fiber.switch方法代码示例

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


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

示例1: test_finished_parent

# 需要导入模块: from fibers import Fiber [as 别名]
# 或者: from fibers.Fiber import switch [as 别名]
 def test_finished_parent(self):
     def f():
         return 42
     g = Fiber(f)
     g.switch()
     self.assertFalse(g.is_alive())
     self.assertRaises(ValueError, Fiber, parent=g)
开发者ID:Dmdv,项目名称:python-fibers,代码行数:9,代码来源:test_fibers.py

示例2: send_exception

# 需要导入模块: from fibers import Fiber [as 别名]
# 或者: from fibers.Fiber import switch [as 别名]
def send_exception(g, exc):
    # note: send_exception(g, exc)  can be now done with  g.throw(exc).
    # the purpose of this test is to explicitely check the propagation rules.
    def crasher(exc):
        raise exc
    g1 = Fiber(target=crasher, args=(exc, ), parent=g)
    g1.switch()
开发者ID:Dmdv,项目名称:python-fibers,代码行数:9,代码来源:test_fibers.py

示例3: f

# 需要导入模块: from fibers import Fiber [as 别名]
# 或者: from fibers.Fiber import switch [as 别名]
 def f():
     try:
         raise ValueError('fun')
     except:
         exc_info = sys.exc_info()
         t = Fiber(h)
         t.switch()
         self.assertEqual(exc_info, sys.exc_info())
         del t
开发者ID:Dmdv,项目名称:python-fibers,代码行数:11,代码来源:test_fibers.py

示例4: test_kwarg_refs

# 需要导入模块: from fibers import Fiber [as 别名]
# 或者: from fibers.Fiber import switch [as 别名]
 def test_kwarg_refs(self):
     kwargs = {'a': 1234}
     refcount_before = sys.getrefcount(kwargs)
     g = Fiber(lambda **x: None, kwargs=kwargs)
     self.assertEqual(sys.getrefcount(kwargs), refcount_before+1)
     g.switch()
     self.assertEqual(sys.getrefcount(kwargs), refcount_before)
     del g
     self.assertEqual(sys.getrefcount(kwargs), refcount_before)
开发者ID:Dmdv,项目名称:python-fibers,代码行数:11,代码来源:test_leaks.py

示例5: test_arg_refs

# 需要导入模块: from fibers import Fiber [as 别名]
# 或者: from fibers.Fiber import switch [as 别名]
 def test_arg_refs(self):
     args = ('a', 'b', 'c')
     refcount_before = sys.getrefcount(args)
     g = Fiber(target=lambda *x: None, args=args)
     self.assertEqual(sys.getrefcount(args), refcount_before+1)
     g.switch()
     self.assertEqual(sys.getrefcount(args), refcount_before)
     del g
     self.assertEqual(sys.getrefcount(args), refcount_before)
开发者ID:Dmdv,项目名称:python-fibers,代码行数:11,代码来源:test_leaks.py

示例6: test_exception

# 需要导入模块: from fibers import Fiber [as 别名]
# 或者: from fibers.Fiber import switch [as 别名]
 def test_exception(self):
     seen = []
     g1 = Fiber(target=fmain, args=(seen, ))
     g2 = Fiber(target=fmain, args=(seen, ))
     g1.switch()
     g2.switch()
     g2.parent = g1
     self.assertEqual(seen, [])
     self.assertRaises(SomeError, g2.switch)
     self.assertEqual(seen, [SomeError])
开发者ID:Dmdv,项目名称:python-fibers,代码行数:12,代码来源:test_fibers.py

示例7: test_simple2

# 需要导入模块: from fibers import Fiber [as 别名]
# 或者: from fibers.Fiber import switch [as 别名]
    def test_simple2(self):
        lst = []

        def f():
            lst.append(1)
            current().parent.switch()
            lst.append(3)
        g = Fiber(f)
        lst.append(0)
        g.switch()
        lst.append(2)
        g.switch()
        lst.append(4)
        self.assertEqual(lst, list(range(5)))
开发者ID:Dmdv,项目名称:python-fibers,代码行数:16,代码来源:test_fibers.py

示例8: test_two_recursive_children

# 需要导入模块: from fibers import Fiber [as 别名]
# 或者: from fibers.Fiber import switch [as 别名]
    def test_two_recursive_children(self):
        lst = []

        def f():
            lst.append(1)
            current().parent.switch()

        def h():
            lst.append(1)
            i = Fiber(f)
            i.switch()
            lst.append(1)
        g = Fiber(h)
        g.switch()
        self.assertEqual(len(lst), 3)
开发者ID:Dmdv,项目名称:python-fibers,代码行数:17,代码来源:test_fibers.py

示例9: test_exc_state

# 需要导入模块: from fibers import Fiber [as 别名]
# 或者: from fibers.Fiber import switch [as 别名]
    def test_exc_state(self):
        def f():
            try:
                raise ValueError('fun')
            except:
                exc_info = sys.exc_info()
                t = Fiber(h)
                t.switch()
                self.assertEqual(exc_info, sys.exc_info())
                del t

        def h():
            self.assertEqual(sys.exc_info(), (None, None, None))

        g = Fiber(f)
        g.switch()
开发者ID:Dmdv,项目名称:python-fibers,代码行数:18,代码来源:test_fibers.py

示例10: test_instance_dict

# 需要导入模块: from fibers import Fiber [as 别名]
# 或者: from fibers.Fiber import switch [as 别名]
 def test_instance_dict(self):
     def f():
         current().test = 42
     def deldict(g):
         del g.__dict__
     def setdict(g, value):
         g.__dict__ = value
     g = Fiber(f)
     self.assertEqual(g.__dict__, {})
     g.switch()
     self.assertEqual(g.test, 42)
     self.assertEqual(g.__dict__, {'test': 42})
     g.__dict__ = g.__dict__
     self.assertEqual(g.__dict__, {'test': 42})
     self.assertRaises(AttributeError, deldict, g)
     self.assertRaises(TypeError, setdict, g, 42)
开发者ID:Dmdv,项目名称:python-fibers,代码行数:18,代码来源:test_fibers.py

示例11: test_kill

# 需要导入模块: from fibers import Fiber [as 别名]
# 或者: from fibers.Fiber import switch [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

示例12: test_val

# 需要导入模块: from fibers import Fiber [as 别名]
# 或者: from fibers.Fiber import switch [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

示例13: test_class

# 需要导入模块: from fibers import Fiber [as 别名]
# 或者: from fibers.Fiber import switch [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

示例14: test_throw_goes_to_original_parent

# 需要导入模块: from fibers import Fiber [as 别名]
# 或者: from fibers.Fiber import switch [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

示例15: test_finalizer_crash

# 需要导入模块: from fibers import Fiber [as 别名]
# 或者: from fibers.Fiber import switch [as 别名]
 def test_finalizer_crash(self):
     # This test is designed to crash when active greenlets
     # are made garbage collectable, until the underlying
     # problem is resolved. How does it work:
     # - order of object creation is important
     # - array is created first, so it is moved to unreachable first
     # - we create a cycle between a greenlet and this array
     # - we create an object that participates in gc, is only
     #   referenced by a greenlet, and would corrupt gc lists
     #   on destruction, the easiest is to use an object with
     #   a finalizer
     # - because array is the first object in unreachable it is
     #   cleared first, which causes all references to greenlet
     #   to disappear and causes greenlet to be destroyed, but since
     #   it is still live it causes a switch during gc, which causes
     #   an object with finalizer to be destroyed, which causes stack
     #   corruption and then a crash
     class object_with_finalizer(object):
         def __del__(self):
             pass
     array = []
     parent = current()
     def greenlet_body():
         current().object = object_with_finalizer()
         try:
             parent.switch()
         finally:
             del current().object
     g = Fiber(greenlet_body)
     g.array = array
     array.append(g)
     g.switch()
     del array
     del g
     current()
     gc.collect()
开发者ID:saghul,项目名称:python-fibers,代码行数:38,代码来源:test_gc.py


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