resourcemonitor#

class resourcemonitor(mem=0.9, cpu=None, time=None, interval=1.0, label=None, start=True, die=True, kill_children=True, kill_parent=False, callback=None, verbose=None)[source]#

Bases: prettyobj

Asynchronously monitor resource (e.g. memory) usage and terminate the process if the specified threshold is exceeded.

Parameters:
  • mem (float) – maximum virtual memory allowed (as a fraction of total RAM)

  • cpu (float) – maximum CPU usage (NB: included for completeness only; typically one would not terminate a process just due to high CPU usage)

  • time (float) – maximum time limit in seconds

  • interval (float) – how frequently to check memory/CPU usage (in seconds)

  • label (str) – an optional label to use while printing out progress

  • start (bool) – whether to start the resource monitor on initialization (else call start())

  • die (bool) – whether to raise an exception if the resource limit is exceeded

  • kill_children (bool) – whether to kill child processes (if False, will not work with multiprocessing)

  • kill_parent (bool) – whether to also kill the parent process (will usually exit Python interpreter in the process)

  • callback (func) – optional callback if the resource limit is exceeded

  • verbose (bool) – detail to print out (default: if exceeded; True: every step; False: no output)

Examples:

# Using with-as:
with sc.resourcemonitor(mem=0.8) as resmon:
    memory_heavy_job()

# As a standalone (don't forget to call stop!)
resmon = sc.resourcemonitor(mem=0.95, cpu=0.9, time=3600, label='Load checker', die=False, callback=post_to_slack)
long_cpu_heavy_job()
resmon.stop()
print(resmon.to_df())
,

Methods

start(label=None)[source]#

Start the monitor running

Parameters:

label (str) – optional label for printing progress

stop()[source]#

Stop the monitor from running

monitor(label=None, *args, **kwargs)[source]#

Actually run the resource monitor

check()[source]#

Check if any limits have been exceeded

kill()[source]#

Kill all processes

to_df()[source]#

Convert the log into a pandas dataframe