From 89446f0c564d0e42447f5d339bc137391f81e777 Mon Sep 17 00:00:00 2001 From: Sven Augustin Date: Thu, 1 Jun 2023 16:35:50 +0200 Subject: [PATCH] allow to set timeout and maxsize; fixed a weird conditional --- bstrd/bscache.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/bstrd/bscache.py b/bstrd/bscache.py index eeeba04..3cab89d 100644 --- a/bstrd/bscache.py +++ b/bstrd/bscache.py @@ -6,10 +6,11 @@ from .prodthread import ProdThread class BSCache: - def __init__(self): + def __init__(self, timeout=1000, maxsize=100): + self.timeout = timeout self.channels = {} self.data = None - self.pt = ProdThread(self.run) + self.pt = ProdThread(self.run, maxsize=maxsize) def __repr__(self): return str(self.data) @@ -26,9 +27,11 @@ class BSCache: def run(self, running): configs = self.channels.values() - with source(channels=configs, receive_timeout=-1) as src: + with source(channels=configs, receive_timeout=self.timeout) as src: while running.is_set(): msg = src.receive() + if msg is None: + continue data = repack(msg) if data: yield data @@ -39,7 +42,7 @@ class BSCache: def get_var(self, name, modulo=None, offset=None): - if name is not "pid" and not name in self.channels: + if name != "pid" and name not in self.channels: if not is_available(name): raise ValueError(f"channel {name} is not available") cfg = make_channel_config(name, modulo, offset)