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


Python fibers.Fiber类代码示例

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


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

示例1: test_finished_parent

 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,代码行数:7,代码来源:test_fibers.py

示例2: send_exception

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,代码行数:7,代码来源:test_fibers.py

示例3: test_kwarg_refs

 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,代码行数:9,代码来源:test_leaks.py

示例4: f

 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,代码行数:9,代码来源:test_fibers.py

示例5: test_arg_refs

 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,代码行数:9,代码来源:test_leaks.py

示例6: test_kill

 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,代码行数:13,代码来源:test_throw.py

示例7: test_class

 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,代码行数:13,代码来源:test_throw.py

示例8: test_simple2

    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,代码行数:14,代码来源:test_fibers.py

示例9: test_two_recursive_children

    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,代码行数:15,代码来源:test_fibers.py

示例10: test_exc_state

    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,代码行数:16,代码来源:test_fibers.py

示例11: test_instance_dict

 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,代码行数:16,代码来源:test_fibers.py

示例12: test_throw_goes_to_original_parent

    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
开发者ID:pombredanne,项目名称:python-fibers,代码行数:19,代码来源:test_throw.py

示例13: switch

 def switch(self):
     if not self._started:
         self.run()
         return
     current = Fiber.current()
     assert current is not self.task, 'Cannot switch to MAIN from MAIN'
     try:
         if self.task.parent is not current:
             current.parent = self.task
     except ValueError:
         pass  # gets raised if there is a Fiber parent cycle
     return self.task.switch()
开发者ID:crudbug,项目名称:evergreen,代码行数:12,代码来源:loop.py

示例14: test_exception

 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,代码行数:10,代码来源:test_fibers.py

示例15: run

 def run(self, mode=RUN_DEFAULT):
     if Fiber.current() is not self.task.parent:
         raise RuntimeError('run() can only be called from MAIN fiber')
     if not self.task.is_alive():
         raise RuntimeError('event loop has already ended')
     if self._started:
         raise RuntimeError('event loop was already started')
     self._started = True
     self._running = True
     self._run_mode = mode
     try:
         self.task.switch()
     finally:
         self._running = False
开发者ID:crudbug,项目名称:evergreen,代码行数:14,代码来源:loop.py


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