Monitor¶
This module provides a monitoring tool for tracking the performance of threaded and process-based generators.
It visualizes queue sizes, production rates, and consumption rates in real-time, either in the terminal or using matplotlib.
Examples¶
>>> from threaded_generator import ThreadedGenerator
>>> import time
>>>
>>> # Create a monitor
>>> monitor = Monitor()
>>>
>>> # Pass the monitor to a generator
>>> gen = ThreadedGenerator(range(100), monitor=monitor, name="MyGen")
>>>
>>> # Start the monitor (usually in a context manager or separate thread)
>>> with monitor:
... for item in gen:
... time.sleep(0.1) # Simulate work
...
# (Terminal output will show a progress bar and stats for "MyGen")
- class Monitor(name='Monitor')[source]¶
Bases:
ThreadA monitoring thread that aggregates and displays statistics for parallel generators.
It runs in the background, consuming statistics pushed by generators via a queue, and updates a live display in the terminal.
- Parameters:
name (str) – The name of the monitor thread. Defaults to “Monitor”.
- drain()[source]¶
Process all currently available items in the statistics queue.
Calculates current throughputs and average queue sizes since the last drain.
- Returns:
A tuple of (throughput_map, average_queue_sizes).
- Return type:
tuple[dict[tuple[str, str], float], dict[str, int]]
- plot()[source]¶
Display live statistics using matplotlib on the main thread.
This method blocks until the plot window is closed. It requires matplotlib to be installed.
- Return type:
None