import asyncio from asyncio.unix_events import SelectorEventLoop class MyLoop(SelectorEventLoop): def __init__(self, *args, **kw): self.calls = 0 SelectorEventLoop.__init__(self, *args, **kw) def _run_once(self): self.calls += 1 SelectorEventLoop._run_once(self) @asyncio.coroutine def compute(x, y): print("Compute %s + %s ..." % (x, y)) yield from asyncio.sleep(1.0) return x + y @asyncio.coroutine def wrapper(coro): print("wrap", coro) yield from coro @asyncio.coroutine def print_sum(x, y): result = yield from wrapper(compute(x, y)) print("%s + %s = %s" % (x, y, result)) loop = MyLoop() asyncio.set_event_loop(loop) loop.run_until_complete(print_sum(1, 2)) print(loop.calls, "calls to _run_once")