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 is end. ok : 1 ok : 2 ok : 3 ok : 4 End ok is end.