#NEWLINE# def _get_memory( pid , timestamps = False , include_children = False ) : #NEWLINE# #INDENT# if ( pid == ( - 1 ) ) : #NEWLINE# #INDENT# pid = os . getpid ( ) #NEWLINE# #UNINDENT# if has_psutil : #NEWLINE# #INDENT# process = psutil . Process ( pid ) #NEWLINE# try : #NEWLINE# #INDENT# meminfo_attr = ( 'memory_info' if hasattr ( process , 'memory_info' ) else 'get_memory_info' ) #NEWLINE# mem = ( getattr ( process , meminfo_attr ) ( ) [ 0 ] / _TWO_20 ) #NEWLINE# if include_children : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# for p in process . get_children ( recursive = True ) : #NEWLINE# #INDENT# mem += ( getattr ( p , meminfo_attr ) ( ) [ 0 ] / _TWO_20 ) #NEWLINE# #UNINDENT# #UNINDENT# except AttributeError : #NEWLINE# #INDENT# for p in process . children ( recursive = True ) : #NEWLINE# #INDENT# mem += ( getattr ( p , meminfo_attr ) ( ) [ 0 ] / _TWO_20 ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# if timestamps : #NEWLINE# #INDENT# return ( mem , time . time ( ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# return mem #NEWLINE# #UNINDENT# #UNINDENT# except psutil . AccessDenied : #NEWLINE# #INDENT# pass #NEWLINE# #UNINDENT# #UNINDENT# if ( os . name == 'posix' ) : #NEWLINE# #INDENT# if include_children : #NEWLINE# #INDENT# raise NotImplementedError ( 'The psutil module is required when to monitor memory usage of children processes' ) #NEWLINE# #UNINDENT# warnings . warn ( 'psutil module not found. memory_profiler will be slow' ) #NEWLINE# out = subprocess . Popen ( [ 'ps' , 'v' , '-p' , str ( pid ) ] , stdout = subprocess . PIPE ) . communicate ( ) [ 0 ] . split ( b'\n' ) #NEWLINE# try : #NEWLINE# #INDENT# vsz_index = out [ 0 ] . split ( ) . index ( b'RSS' ) #NEWLINE# mem = ( float ( out [ 1 ] . split ( ) [ vsz_index ] ) / 1024 ) #NEWLINE# if timestamps : #NEWLINE# #INDENT# return ( timestamps , time . time ( ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# return mem #NEWLINE# #UNINDENT# #UNINDENT# except : #NEWLINE# #INDENT# if timestamps : #NEWLINE# #INDENT# return ( ( - 1 ) , time . time ( ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# return ( - 1 ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# raise NotImplementedError ( 'The psutil module is required for non-unix platforms' )