HWRF  trunk@4391
Public Member Functions | Public Attributes | List of all members
produtil.rusage.RUsage Class Reference

Contains resource usage (rusage) information that can be used with a Python "with" construct to collect the resources utilized by a block of code, or group of subprocesses executing during that block. More...

Detailed Description

Contains resource usage (rusage) information that can be used with a Python "with" construct to collect the resources utilized by a block of code, or group of subprocesses executing during that block.

Example:

1 with produtil.rusage.RUsage(logger=logging.getLogger("usage")):
2  ... do things ...
3 ... stop doing things ...

Just after the "with" block exits, the resource usage is printed to the given logger. The information can be retained for inspection instead:

1 u=produtil.rusage.RUsage(logger=logging.getLogger("usage"))
2 with u:
3  ... do things ...
4 ... stop doing things ...
5 # u.rusage_before is a dict of resource usage before the block
6 # u.time_before contains the time before the block
7 # u.rusage_after contains the resource usage at the end of the block
8 # u.time_after contains the time after the block

Note that the logger is optional: without it, nothing is logged.

Definition at line 175 of file rusage.py.

Inheritance diagram for produtil.rusage.RUsage:

Public Member Functions

def __init__
 Creates an RUsage object for input to a "with" statement. More...
 
def who (self)
 The "who" parameter to the constructor, which selects whether the usage measured should be of the child processes (RUSAGE_CHILDREN) or this process (RUSAGE_SELF) . More...
 
def pagesize (self)
 System page size in bytes from resource.getpagesize(). More...
 
def __enter__ (self)
 Gets the resource usage and time at the top of the "with" block. More...
 
def __exit__ (self, type, value, tb)
 Gets the resource usage and time at the end of a "with" block. More...
 
def report (self)
 Generates a string report of the resource usage utilized. More...
 
def __str__ (self)
 Generates a string report of the resource usage utilized. More...
 

Public Attributes

 logger
 The logging.Logger for log messages.
 
 rusage_before
 Resource usage before monitoring began.
 
 rusage_after
 The resource usage after monitoring ended.
 
 time_before
 The current time before usage monitoring began.
 
 time_after
 The current time after monitoring ended. More...
 

Constructor & Destructor Documentation

def produtil.rusage.RUsage.__init__ (   self,
  who = resource.RUSAGE_CHILDREN,
  logger = None 
)

Creates an RUsage object for input to a "with" statement.

Parameters
whoPass who=resource.RUSAGE_SELF to get usage on this process or rusage.RUSAGE_CHILDREN (the default) to get resource usage on child processes.
loggera logging.Logger for log messages

Definition at line 203 of file rusage.py.

Member Function Documentation

def produtil.rusage.RUsage.__enter__ (   self)

Gets the resource usage and time at the top of the "with" block.

This function is called automatically by the Python interpreter at the beginning of a "with" block.

Definition at line 245 of file rusage.py.

def produtil.rusage.RUsage.__exit__ (   self,
  type,
  value,
  tb 
)

Gets the resource usage and time at the end of a "with" block.

This is called automatically by Python at the end of a "with" block.

Parameters
type,value,tbexception information

Definition at line 251 of file rusage.py.

def produtil.rusage.RUsage.__str__ (   self)

Generates a string report of the resource usage utilized.

Definition at line 278 of file rusage.py.

def produtil.rusage.RUsage.pagesize (   self)

System page size in bytes from resource.getpagesize().

This is needed to interpret return values.

Definition at line 241 of file rusage.py.

def produtil.rusage.RUsage.report (   self)

Generates a string report of the resource usage utilized.

Accessible via str(self).

Definition at line 261 of file rusage.py.

Referenced by produtil.rusage.RUsage.__exit__(), and produtil.rusage.RUsage.__str__().

def produtil.rusage.RUsage.who (   self)

The "who" parameter to the constructor, which selects whether the usage measured should be of the child processes (RUSAGE_CHILDREN) or this process (RUSAGE_SELF) .

See init for details.

Definition at line 234 of file rusage.py.

Member Data Documentation

produtil.rusage.RUsage.time_after

The current time after monitoring ended.

Definition at line 214 of file rusage.py.

Referenced by produtil.rusage.RUsage.__exit__(), and produtil.rusage.RUsage.report().


The documentation for this class was generated from the following file: