From 30a05867326eec2f9c2c2254126fe5bb0e118848 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Fri, 2 May 2014 02:45:06 +0300 Subject: [PATCH] asyncio_slow: Implement loop.stop(). --- asyncio_slow/asyncio_slow.py | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/asyncio_slow/asyncio_slow.py b/asyncio_slow/asyncio_slow.py index e0a89433..e82afcf1 100644 --- a/asyncio_slow/asyncio_slow.py +++ b/asyncio_slow/asyncio_slow.py @@ -6,9 +6,10 @@ log = logging.getLogger("asyncio") # Workaround for not being able to subclass builtin types -DoneException = AssertionError +class LoopStop(Exception): + pass -class InvalidStateError: +class InvalidStateError(Exception): pass # Object not matching any other object @@ -32,21 +33,23 @@ class EventLoop: def run_forever(self): while self.q: c = self.q.pop(0) - c[0](*c[1]) + try: + c[0](*c[1]) + except LoopStop: + return # I mean, forever while True: time.sleep(1) - def run_until_complete(self, coro): - def _cb(val): - raise DoneException + def stop(self): + def _cb(): + raise LoopStop + self.call_soon(_cb) + def run_until_complete(self, coro): t = async(coro) - t.add_done_callback(_cb) - try: - self.run_forever() - except DoneException: - pass + t.add_done_callback(lambda a: self.stop()) + self.run_forever() def close(self): pass