From b732bc8b38dd57dd108a73a129976dbd02890a31 Mon Sep 17 00:00:00 2001 From: Antoine Millet Date: Wed, 5 Oct 2011 18:06:02 +0200 Subject: [PATCH] Added AsyncManager.iter iterator. --- sjrpc/core/async.py | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/sjrpc/core/async.py b/sjrpc/core/async.py index 56e945d..058b260 100644 --- a/sjrpc/core/async.py +++ b/sjrpc/core/async.py @@ -76,7 +76,19 @@ class AsyncWatcher(object): >>> for msg in watcher.wait(timeout=60): >>> process(msg) ''' - responses = [] + return list(self.iter(timeout, max_wait)) + + def iter(self, timeout=None, max_wait=None): + ''' + Work like :meth:`AsyncWatcher.wait` but return an iterable, instead of + a list object. + + .. note:: + + Responses are yielded by the iterable when they are received, so you + can start the processing of response before receiving all. + ''' + while self.remains: try: dt, response = self._get_in_queue(timeout=timeout) @@ -84,12 +96,10 @@ class AsyncWatcher(object): break if timeout is not None: timeout -= dt - responses.append(response) self._expected_responses.remove(response['id']) + yield response # Check for max_wait: if max_wait is not None: max_wait -= 1 if not max_wait: break - return responses - -- GitLab