3
3
# .. we'll use this to pass it to the child script ..
4
4
_CLEAN_GLOBALS = globals ().copy ()
5
5
6
- __version__ = '0.59 .0'
6
+ __version__ = '0.60 .0'
7
7
8
8
_CMD_USAGE = "python -m memory_profiler script_file.py"
9
9
23
23
import traceback
24
24
import warnings
25
25
26
-
27
26
if sys .platform == "win32" :
28
27
# any value except signal.CTRL_C_EVENT and signal.CTRL_BREAK_EVENT
29
28
# 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):
106
105
for child in getattr (process , children_attr )(recursive = True ):
107
106
if isinstance (memory_metric , str ):
108
107
meminfo = getattr (child , meminfo_attr )()
109
- yield getattr (meminfo , memory_metric ) / _TWO_20
108
+ yield child . pid , getattr (meminfo , memory_metric ) / _TWO_20
110
109
else :
111
- yield getattr (child , meminfo_attr )()[memory_metric ] / _TWO_20
110
+ yield child . pid , getattr (child , meminfo_attr )()[memory_metric ] / _TWO_20
112
111
except (psutil .NoSuchProcess , psutil .AccessDenied ):
113
112
# https://github.com/fabianp/memory_profiler/issues/71
114
- yield 0.0
113
+ yield ( 0 , 0.0 )
115
114
116
115
117
116
def _get_memory (pid , backend , timestamps = False , include_children = False , filename = None ):
@@ -139,7 +138,7 @@ def ps_util_tool():
139
138
else 'get_memory_info'
140
139
mem = getattr (process , meminfo_attr )()[0 ] / _TWO_20
141
140
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 )] )
143
142
if timestamps :
144
143
return mem , time .time ()
145
144
else :
@@ -166,7 +165,7 @@ def _ps_util_full_tool(memory_metric):
166
165
mem = getattr (meminfo , memory_metric ) / _TWO_20
167
166
168
167
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 )] )
170
169
171
170
if timestamps :
172
171
return mem , time .time ()
@@ -411,13 +410,13 @@ def memory_usage(proc=-1, interval=.1, timeout=None, timestamps=False,
411
410
412
411
# Write children to the stream file
413
412
if multiprocess :
414
- for idx , chldmem in enumerate ( _get_child_memory (proc .pid ) ):
413
+ for idx , chldmem in _get_child_memory (proc .pid ):
415
414
stream .write ("CHLD {0} {1:.6f} {2:.4f}\n " .format (idx , chldmem , time .time ()))
416
415
else :
417
416
# Create a nested list with the child memory
418
417
if multiprocess :
419
418
mem_usage = [mem_usage ]
420
- for chldmem in _get_child_memory (proc .pid ):
419
+ for _ , chldmem in _get_child_memory (proc .pid ):
421
420
mem_usage .append (chldmem )
422
421
423
422
# Append the memory usage to the return value
@@ -455,13 +454,13 @@ def memory_usage(proc=-1, interval=.1, timeout=None, timestamps=False,
455
454
456
455
# Write children to the stream file
457
456
if multiprocess :
458
- for idx , chldmem in enumerate ( _get_child_memory (proc ) ):
457
+ for idx , chldmem in _get_child_memory (proc ):
459
458
stream .write ("CHLD {0} {1:.6f} {2:.4f}\n " .format (idx , chldmem , time .time ()))
460
459
else :
461
460
# Create a nested list with the child memory
462
461
if multiprocess :
463
462
mem_usage = [mem_usage ]
464
- for chldmem in _get_child_memory (proc ):
463
+ for _ , chldmem in _get_child_memory (proc ):
465
464
mem_usage .append (chldmem )
466
465
467
466
# Append the memory usage to the return value
@@ -1248,7 +1247,6 @@ def exec_with_profiler(filename, profiler, backend, passed_args=[]):
1248
1247
try :
1249
1248
if _backend == 'tracemalloc' and has_tracemalloc :
1250
1249
tracemalloc .start ()
1251
-
1252
1250
with io .open (filename , encoding = 'utf-8' ) as f :
1253
1251
exec (compile (f .read (), filename , 'exec' ), ns , ns )
1254
1252
finally :
0 commit comments