@@ -240,8 +240,8 @@ def run(self):
240240
241241
242242def memory_usage (proc = - 1 , interval = .1 , timeout = None , timestamps = False ,
243- include_children = False , max_usage = False , retval = False ,
244- stream = None , backend = None ):
243+ include_children = False , multiprocess = False , max_usage = False ,
244+ retval = False , stream = None , backend = None ):
245245 """
246246 Return the memory usage of a process or piece of code
247247
@@ -272,6 +272,12 @@ def memory_usage(proc=-1, interval=.1, timeout=None, timestamps=False,
272272 timestamps : bool, optional
273273 if True, timestamps of memory usage measurement are collected as well.
274274
275+ include_children : bool, optional
276+ if True, sum the memory of all forked processes as well
277+
278+ multiprocess : bool, optional
279+ if True, track the memory usage of all forked processes.
280+
275281 stream : File
276282 if stream is a File opened with write access, then results are written
277283 to this file instead of stored in memory and returned at the end of
@@ -343,10 +349,18 @@ def memory_usage(proc=-1, interval=.1, timeout=None, timestamps=False,
343349 mem_usage = _get_memory (
344350 proc .pid , backend , timestamps = timestamps ,
345351 include_children = include_children )
352+
346353 if stream is not None :
347354 stream .write ("MEM {0:.6f} {1:.4f}\n " .format (* mem_usage ))
355+
356+ # Only write children to the stream file, warn if appending to the return.
357+ if multiprocess :
358+ for idx , chldmem in enumerate (_get_child_memory (proc .pid )):
359+ stream .write ("CHLD {0} {1:.6f} {2:.4f}\n " .format (idx , chldmem , time .time ()))
348360 else :
349361 ret .append (mem_usage )
362+ if multiprocess :
363+ warnings .warn ("use include_children not multiprocess without a stream" )
350364 else :
351365 ret = max (ret ,
352366 _get_memory (
@@ -377,8 +391,16 @@ def memory_usage(proc=-1, interval=.1, timeout=None, timestamps=False,
377391 include_children = include_children )
378392 if stream is not None :
379393 stream .write ("MEM {0:.6f} {1:.4f}\n " .format (* mem_usage ))
394+
395+ # Only write children to the stream file, warn if appending to the return.
396+ if multiprocess :
397+ for idx , chldmem in enumerate (_get_child_memory (proc .pid )):
398+ stream .write ("CHLD {0} {1:.6f} {2:.4f}\n " .format (idx , chldmem , time .time ()))
380399 else :
381400 ret .append (mem_usage )
401+
402+ if multiprocess :
403+ warnings .warn ("use include_children not multiprocess without a stream" )
382404 else :
383405 ret = max ([ret ,
384406 _get_memory (proc , backend , include_children = include_children )
0 commit comments