Skip to content

Commit aab15ed

Browse files
committed
merge mpmprof into mprof
1 parent bdf9995 commit aab15ed

4 files changed

Lines changed: 53 additions & 316 deletions

File tree

examples/multiprocessing_example.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
An undecorated example of a script that allocates memory in multiprocessing
33
workers to demonstrate the use of memory_profiler with multiple processes.
44
5-
Run this script with mprof run -C python multiprocessing_example.py
5+
Run this script with mprof run -M python multiprocessing_example.py
66
You can then visualize the usage with mprof plot.
77
"""
88

memory_profiler.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,8 +240,8 @@ def run(self):
240240

241241

242242
def 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)

mpmprof

Lines changed: 0 additions & 310 deletions
This file was deleted.

0 commit comments

Comments
 (0)