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