本文整理汇总了Python中fibers.Fiber.is_alive方法的典型用法代码示例。如果您正苦于以下问题:Python Fiber.is_alive方法的具体用法?Python Fiber.is_alive怎么用?Python Fiber.is_alive使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类fibers.Fiber
的用法示例。
在下文中一共展示了Fiber.is_alive方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_finished_parent
# 需要导入模块: from fibers import Fiber [as 别名]
# 或者: from fibers.Fiber import is_alive [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)
示例2: test_two_children
# 需要导入模块: from fibers import Fiber [as 别名]
# 或者: from fibers.Fiber import is_alive [as 别名]
def test_two_children(self):
lst = []
def f():
lst.append(1)
current().parent.switch()
lst.extend([1, 1])
g = Fiber(f)
h = Fiber(f)
g.switch()
self.assertEqual(len(lst), 1)
h.switch()
self.assertEqual(len(lst), 2)
h.switch()
self.assertEqual(len(lst), 4)
self.assertEqual(h.is_alive(), False)
g.switch()
self.assertEqual(len(lst), 6)
self.assertEqual(g.is_alive(), False)
示例3: test_throw_goes_to_original_parent
# 需要导入模块: from fibers import Fiber [as 别名]
# 或者: from fibers.Fiber import is_alive [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
示例4: test_throw_goes_to_original_parent2
# 需要导入模块: from fibers import Fiber [as 别名]
# 或者: from fibers.Fiber import is_alive [as 别名]
def test_throw_goes_to_original_parent2(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)
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())
示例5: test_kill
# 需要导入模块: from fibers import Fiber [as 别名]
# 或者: from fibers.Fiber import is_alive [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())
示例6: EventLoop
# 需要导入模块: from fibers import Fiber [as 别名]
# 或者: from fibers.Fiber import is_alive [as 别名]
#.........这里部分代码省略.........
signal_h.close()
raise RuntimeError(str(e))
else:
self._signals.setdefault(sig, set()).add(signal_h)
return handler
def remove_signal_handler(self, sig):
self._validate_signal(sig)
try:
handles = self._signals.pop(sig)
except KeyError:
return False
for signal_h in handles:
del signal_h.handler
signal_h.close()
return True
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()
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
def run_forever(self):
self.run(mode=RUN_FOREVER)
def stop(self):
if not self._started:
raise RuntimeError('event loop has not been started yet')
if self._loop:
self._loop.stop()
def destroy(self):
if self._running:
raise RuntimeError('destroy() cannot be called while the loop is running')
if self._destroyed:
raise RuntimeError('Event loop already destroyed')
loop = getattr(_tls, 'loop', None)
if loop is not self:
raise RuntimeError('destroy() can only be called from the same thread were the event loop was created')
del _tls.loop, loop