always accumulate as much as possible
This commit is contained in:
parent
b26a4e7646
commit
853d25c51a
1 changed files with 14 additions and 12 deletions
|
@ -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()
|
||||
|
||||
|
|
Loading…
Reference in a new issue