def inside(name):
for x in range(5):
print(name,':',x)
yield
def broken():
print('Begin broken')
yield inside('broken')
print('End broken')
def ok():
print('Begin ok')
yield from inside('ok')
print('End ok')
class Eventloop:
coroutines = []
def run(self):
while True:
if len(self.coroutines) == 0:
break
for coro in self.coroutines:
try:
next(coro)
except StopIteration as e:
print(coro, 'is end.')
self.coroutines.remove(coro)
def add_coroutine(self, coro):
self.coroutines.append(coro())
loop = Eventloop()
loop.add_coroutine(broken)
loop.add_coroutine(ok)
if __name__ == '__main__':
loop.run()
Begin broken
Begin ok
ok : 0
End broken
<generator object broken at 0x7f30bf3068e0> is end.
ok : 1
ok : 2
ok : 3
ok : 4
End ok
<generator object ok at 0x7f30bf306990> is end.