summaryrefslogtreecommitdiffhomepage
path: root/libs/tqdm/_monitor.py
diff options
context:
space:
mode:
Diffstat (limited to 'libs/tqdm/_monitor.py')
-rw-r--r--libs/tqdm/_monitor.py32
1 files changed, 14 insertions, 18 deletions
diff --git a/libs/tqdm/_monitor.py b/libs/tqdm/_monitor.py
index e1e257069..f71aa5681 100644
--- a/libs/tqdm/_monitor.py
+++ b/libs/tqdm/_monitor.py
@@ -1,7 +1,8 @@
+import atexit
from threading import Event, Thread, current_thread
from time import time
from warnings import warn
-import atexit
+
__all__ = ["TMonitor", "TqdmSynchronisationWarning"]
@@ -21,29 +22,19 @@ class TMonitor(Thread):
----------
tqdm_cls : class
tqdm class to use (can be core tqdm or a submodule).
- sleep_interval : fload
+ sleep_interval : float
Time to sleep between monitoring checks.
"""
-
- # internal vars for unit testing
- _time = None
- _event = None
+ _test = {} # internal vars for unit testing
def __init__(self, tqdm_cls, sleep_interval):
Thread.__init__(self)
self.daemon = True # kill thread when main killed (KeyboardInterrupt)
- self.was_killed = Event()
self.woken = 0 # last time woken up, to sync with monitor
self.tqdm_cls = tqdm_cls
self.sleep_interval = sleep_interval
- if TMonitor._time is not None:
- self._time = TMonitor._time
- else:
- self._time = time
- if TMonitor._event is not None:
- self._event = TMonitor._event
- else:
- self._event = Event
+ self._time = self._test.get("time", time)
+ self.was_killed = self._test.get("Event", Event)()
atexit.register(self.exit)
self.start()
@@ -82,18 +73,23 @@ class TMonitor(Thread):
return
# Only if mininterval > 1 (else iterations are just slow)
# and last refresh exceeded maxinterval
- if instance.miniters > 1 and \
- (cur_t - instance.last_print_t) >= \
- instance.maxinterval:
+ if (
+ instance.miniters > 1
+ and (cur_t - instance.last_print_t) >= instance.maxinterval
+ ):
# force bypassing miniters on next iteration
# (dynamic_miniters adjusts mininterval automatically)
instance.miniters = 1
# Refresh now! (works only for manual tqdm)
instance.refresh(nolock=True)
+ # Remove accidental long-lived strong reference
+ del instance
if instances != self.get_instances(): # pragma: nocover
warn("Set changed size during iteration" +
" (see https://github.com/tqdm/tqdm/issues/481)",
TqdmSynchronisationWarning, stacklevel=2)
+ # Remove accidental long-lived strong references
+ del instances
def report(self):
return not self.was_killed.is_set()