From 853d25c51a94fd4dde9c0508e4a7c963fd71e52a Mon Sep 17 00:00:00 2001 From: Connor Olding Date: Fri, 4 Sep 2020 13:50:08 +0200 Subject: [PATCH] always accumulate as much as possible --- respodns/ip_util.py | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/respodns/ip_util.py b/respodns/ip_util.py index e9ef747..33b05ee 100644 --- a/respodns/ip_util.py +++ b/respodns/ip_util.py @@ -45,10 +45,10 @@ def ip_reader_worker(fp, queue): class IpReader: def __init__(self, *paths_and_handles): - from queue import Queue + from queue import SimpleQueue self.fps = paths_and_handles - self.queue = Queue() + self.queue = SimpleQueue() self.threads = [] self.total = 0 @@ -56,23 +56,25 @@ class IpReader: return any(thread.is_alive() for thread in self.threads) def __iter__(self): - from queue import Empty + from queue import SimpleQueue, Empty from sys import stderr def _next(): + results = SimpleQueue() while self.is_running() or not self.queue.empty(): - results = [] - if self.queue.empty(): + if self.queue.empty() and results.empty(): try: - results.append(self.queue.get(timeout=1.0)) + results.put(self.queue.get(timeout=1.0)) + self.total += 1 except Empty: print("blocking on IpReader", file=stderr) - else: - while not self.queue.empty(): - results.append(self.queue.get()) - self.total += len(results) - for res in results: - yield res + while not self.queue.empty(): + results.put(self.queue.get()) + self.total += 1 + if not results.empty(): + yield results.get() + while not results.empty(): + yield results.get() return _next()