Skip to content

Commit 3ac0040

Browse files
authored
Merge pull request #354 from IMikhelson/process_pid
Store child processes by PID
2 parents e430342 + 3faea8a commit 3ac0040

File tree

1 file changed

+10
-12
lines changed

1 file changed

+10
-12
lines changed

memory_profiler.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# .. we'll use this to pass it to the child script ..
44
_CLEAN_GLOBALS = globals().copy()
55

6-
__version__ = '0.59.0'
6+
__version__ = '0.60.0'
77

88
_CMD_USAGE = "python -m memory_profiler script_file.py"
99

@@ -23,7 +23,6 @@
2323
import traceback
2424
import warnings
2525

26-
2726
if sys.platform == "win32":
2827
# any value except signal.CTRL_C_EVENT and signal.CTRL_BREAK_EVENT
2928
# can be used to kill a process unconditionally in Windows
@@ -106,12 +105,12 @@ def _get_child_memory(process, meminfo_attr=None, memory_metric=0):
106105
for child in getattr(process, children_attr)(recursive=True):
107106
if isinstance(memory_metric, str):
108107
meminfo = getattr(child, meminfo_attr)()
109-
yield getattr(meminfo, memory_metric) / _TWO_20
108+
yield child.pid, getattr(meminfo, memory_metric) / _TWO_20
110109
else:
111-
yield getattr(child, meminfo_attr)()[memory_metric] / _TWO_20
110+
yield child.pid, getattr(child, meminfo_attr)()[memory_metric] / _TWO_20
112111
except (psutil.NoSuchProcess, psutil.AccessDenied):
113112
# https://github.com/fabianp/memory_profiler/issues/71
114-
yield 0.0
113+
yield (0, 0.0)
115114

116115

117116
def _get_memory(pid, backend, timestamps=False, include_children=False, filename=None):
@@ -139,7 +138,7 @@ def ps_util_tool():
139138
else 'get_memory_info'
140139
mem = getattr(process, meminfo_attr)()[0] / _TWO_20
141140
if include_children:
142-
mem += sum(_get_child_memory(process, meminfo_attr))
141+
mem += sum([mem for (pid, mem) in _get_child_memory(process, meminfo_attr)])
143142
if timestamps:
144143
return mem, time.time()
145144
else:
@@ -166,7 +165,7 @@ def _ps_util_full_tool(memory_metric):
166165
mem = getattr(meminfo, memory_metric) / _TWO_20
167166

168167
if include_children:
169-
mem += sum(_get_child_memory(process, meminfo_attr, memory_metric))
168+
mem += sum([mem for (pid, mem) in _get_child_memory(process, meminfo_attr, memory_metric)])
170169

171170
if timestamps:
172171
return mem, time.time()
@@ -411,13 +410,13 @@ def memory_usage(proc=-1, interval=.1, timeout=None, timestamps=False,
411410

412411
# Write children to the stream file
413412
if multiprocess:
414-
for idx, chldmem in enumerate(_get_child_memory(proc.pid)):
413+
for idx, chldmem in _get_child_memory(proc.pid):
415414
stream.write("CHLD {0} {1:.6f} {2:.4f}\n".format(idx, chldmem, time.time()))
416415
else:
417416
# Create a nested list with the child memory
418417
if multiprocess:
419418
mem_usage = [mem_usage]
420-
for chldmem in _get_child_memory(proc.pid):
419+
for _, chldmem in _get_child_memory(proc.pid):
421420
mem_usage.append(chldmem)
422421

423422
# Append the memory usage to the return value
@@ -455,13 +454,13 @@ def memory_usage(proc=-1, interval=.1, timeout=None, timestamps=False,
455454

456455
# Write children to the stream file
457456
if multiprocess:
458-
for idx, chldmem in enumerate(_get_child_memory(proc)):
457+
for idx, chldmem in _get_child_memory(proc):
459458
stream.write("CHLD {0} {1:.6f} {2:.4f}\n".format(idx, chldmem, time.time()))
460459
else:
461460
# Create a nested list with the child memory
462461
if multiprocess:
463462
mem_usage = [mem_usage]
464-
for chldmem in _get_child_memory(proc):
463+
for _, chldmem in _get_child_memory(proc):
465464
mem_usage.append(chldmem)
466465

467466
# Append the memory usage to the return value
@@ -1248,7 +1247,6 @@ def exec_with_profiler(filename, profiler, backend, passed_args=[]):
12481247
try:
12491248
if _backend == 'tracemalloc' and has_tracemalloc:
12501249
tracemalloc.start()
1251-
12521250
with io.open(filename, encoding='utf-8') as f:
12531251
exec(compile(f.read(), filename, 'exec'), ns, ns)
12541252
finally:

0 commit comments

Comments
 (0)