Skip to content

Commit 9c62f0a

Browse files
committed
return child as nested list from memory_usage
1 parent 099c5c8 commit 9c62f0a

2 files changed

Lines changed: 19 additions & 10 deletions

File tree

README.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,9 +191,9 @@ This will create a plot using matplotlib similar to this:
191191
:height: 350px
192192

193193
You can combine both the ``include_children`` and ``multiprocess`` flags to show
194-
the total memory of the program as well as each child individually.
195-
196-
.. warning:: Currently the child tracking only works if a ``stream`` is provided to the ``profile`` (e.g. from the command line or in the decorator). If you are using the API to retrieve values then the flag will not do anything.
194+
the total memory of the program as well as each child individually. If using
195+
the API directly, note that the return from ``memory_usage`` will include the
196+
child memory along with an index identifying the specific child.
197197

198198
Setting debugger breakpoints
199199
=============================

memory_profiler.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -353,14 +353,19 @@ def memory_usage(proc=-1, interval=.1, timeout=None, timestamps=False,
353353
if stream is not None:
354354
stream.write("MEM {0:.6f} {1:.4f}\n".format(*mem_usage))
355355

356-
# Only write children to the stream file, warn if appending to the return.
356+
# Write children to the stream file
357357
if multiprocess:
358358
for idx, chldmem in enumerate(_get_child_memory(proc.pid)):
359359
stream.write("CHLD {0} {1:.6f} {2:.4f}\n".format(idx, chldmem, time.time()))
360360
else:
361-
ret.append(mem_usage)
361+
# Create a nested list with the child memory
362362
if multiprocess:
363-
warnings.warn("use include_children not multiprocess without a stream")
363+
mem_usage = [mem_usage]
364+
for chldmem in _get_child_memory(proc.pid):
365+
mem_usage.append(chldmem)
366+
367+
# Append the memory usage to the return value
368+
ret.append(mem_usage)
364369
else:
365370
ret = max(ret,
366371
_get_memory(
@@ -392,15 +397,19 @@ def memory_usage(proc=-1, interval=.1, timeout=None, timestamps=False,
392397
if stream is not None:
393398
stream.write("MEM {0:.6f} {1:.4f}\n".format(*mem_usage))
394399

395-
# Only write children to the stream file, warn if appending to the return.
400+
# Write children to the stream file
396401
if multiprocess:
397402
for idx, chldmem in enumerate(_get_child_memory(proc.pid)):
398403
stream.write("CHLD {0} {1:.6f} {2:.4f}\n".format(idx, chldmem, time.time()))
399404
else:
400-
ret.append(mem_usage)
401-
405+
# Create a nested list with the child memory
402406
if multiprocess:
403-
warnings.warn("use include_children not multiprocess without a stream")
407+
mem_usage = [mem_usage]
408+
for chldmem in _get_child_memory(proc.pid):
409+
mem_usage.append(chldmem)
410+
411+
# Append the memory usage to the return value
412+
ret.append(mem_usage)
404413
else:
405414
ret = max([ret,
406415
_get_memory(proc, backend, include_children=include_children)

0 commit comments

Comments
 (0)