mlens.utils.utils module¶
ML-ENSEMBLE
author: | Sebastian Flennerhag |
---|---|
copyright: | 2017 |
licence: | MIT |
-
class
mlens.utils.utils.
CMLog
(verbose=False)[source]¶ Bases:
object
CPU and Memory logger.
Class for starting a monitor job of CPU and memory utilization in the background in a Python script. The
monitor
class records thecpu_percent
,rss
andvms
as collected by the psutil library for the parent process’ pid.CPU usage and memory utilization are stored as attributes in numpy arrays.
Examples
>>> from time import sleep >>> from mlens.utils.utils import CMLog >>> cm = CMLog(verbose=True) >>> cm.monitor(2, 0.5) >>> _ = [i for i in range(10000000)] >>> >>> # Collecting before completion triggers a message but no error >>> cm._collect() >>> >>> sleep(2) >>> cm._collect() >>> print('CPU usage:') >>> cm.cpu [CMLog] Monitoring for 2 seconds with checks every 0.5 seconds. [CMLog] Job not finished. Cannot _collect yet. [CMLog] Collecting... done. Read 4 lines in 0.000 seconds. CPU usage: array([ 50. , 22.4, 6. , 11.9])
Raises: ImportError : – Depends on psutil. If not installed, raises ImportError on instantiation. Parameters: verbose (bool) – whether to notify of job start. -
collect
()[source]¶ Collect monitored data.
Once a monitor job finishes, call
_collect
to read the CPU and memory usage into python objects in the current process. If called before the job finishes, _collect issues a print statement to try again later, but no warning or error is raised.
-
monitor
(stop=None, ival=0.1, kill=True)[source]¶ Start monitoring CPU and memory usage.
Parameters: - stop (float or None (default = None)) – seconds to monitor for. If None, monitors until
_collect
is called. - ival (float (default=0.1)) – interval of monitoring.
- kill (bool (default = True)) – whether to kill the monitoring job if
_collect
is called before timeout (stop
). If set to False, calling_collect
will cause the instance to wait until the job completes.
- stop (float or None (default = None)) – seconds to monitor for. If None, monitors until
-