@@ -1204,6 +1204,7 @@ def _read(self, drain):
1204
1204
self ._lastidx = len (self ._rows )
1205
1205
1206
1206
1207
+ # Get number of threads for process
1207
1208
def _get_num_threads (proc ):
1208
1209
'''
1209
1210
'''
@@ -1223,6 +1224,29 @@ def _get_num_threads(proc):
1223
1224
return num_threads
1224
1225
1225
1226
1227
+ # Get max resources used for process
1228
+ def _get_max_resources_used (proc , mem_mb , num_threads , poll = False ):
1229
+ '''
1230
+ docstring
1231
+ '''
1232
+
1233
+ # Import packages
1234
+ from memory_profiler import _get_memory
1235
+ import psutil
1236
+
1237
+ try :
1238
+ mem_mb = max (mem_mb , _get_memory (proc .pid , include_children = True ))
1239
+ num_threads = max (num_threads , _get_num_threads (psutil .Process (proc .pid )))
1240
+ if poll :
1241
+ proc .poll ()
1242
+ except Exception as exc :
1243
+ iflogger .info ('Could not get resources used by process. Error: %s' \
1244
+ % exc )
1245
+
1246
+ # Return resources
1247
+ return mem_mb , num_threads
1248
+
1249
+
1226
1250
def run_command (runtime , output = None , timeout = 0.01 , redirect_x = False ):
1227
1251
"""Run a command, read stdout and stderr, prefix with timestamp.
1228
1252
@@ -1231,7 +1255,7 @@ def run_command(runtime, output=None, timeout=0.01, redirect_x=False):
1231
1255
1232
1256
# Import packages
1233
1257
try :
1234
- from memory_profiler import _get_memory
1258
+ import memory_profiler
1235
1259
import psutil
1236
1260
mem_prof = True
1237
1261
except :
@@ -1273,7 +1297,6 @@ def run_command(runtime, output=None, timeout=0.01, redirect_x=False):
1273
1297
# Init variables for memory profiling
1274
1298
mem_mb = - 1
1275
1299
num_threads = - 1
1276
- interval = 1
1277
1300
1278
1301
if output == 'stream' :
1279
1302
streams = [Stream ('stdout' , proc .stdout ), Stream ('stderr' , proc .stderr )]
@@ -1292,8 +1315,8 @@ def _process(drain=0):
1292
1315
stream .read (drain )
1293
1316
while proc .returncode is None :
1294
1317
if mem_prof :
1295
- mem_mb = max ( mem_mb , _get_memory ( proc . pid , include_children = True ))
1296
- num_threads = max ( num_threads , _get_num_threads ( psutil . Process ( proc . pid )) )
1318
+ mem_mb , num_threads = \
1319
+ _get_max_resources_used ( proc , mem_mb , num_threads )
1297
1320
proc .poll ()
1298
1321
_process ()
1299
1322
_process (drain = 1 )
@@ -1311,9 +1334,8 @@ def _process(drain=0):
1311
1334
if output == 'allatonce' :
1312
1335
if mem_prof :
1313
1336
while proc .returncode is None :
1314
- mem_mb = max (mem_mb , _get_memory (proc .pid , include_children = True ))
1315
- num_threads = max (num_threads , _get_num_threads (psutil .Process (proc .pid )))
1316
- proc .poll ()
1337
+ mem_mb , num_threads = \
1338
+ _get_max_resources_used (proc , mem_mb , num_threads , poll = True )
1317
1339
stdout , stderr = proc .communicate ()
1318
1340
if stdout and isinstance (stdout , bytes ):
1319
1341
try :
@@ -1332,9 +1354,8 @@ def _process(drain=0):
1332
1354
if output == 'file' :
1333
1355
if mem_prof :
1334
1356
while proc .returncode is None :
1335
- mem_mb = max (mem_mb , _get_memory (proc .pid , include_children = True ))
1336
- num_threads = max (num_threads , _get_num_threads (psutil .Process (proc .pid )))
1337
- proc .poll ()
1357
+ mem_mb , num_threads = \
1358
+ _get_max_resources_used (proc , mem_mb , num_threads , poll = True )
1338
1359
ret_code = proc .wait ()
1339
1360
stderr .flush ()
1340
1361
stdout .flush ()
@@ -1344,9 +1365,8 @@ def _process(drain=0):
1344
1365
if output == 'none' :
1345
1366
if mem_prof :
1346
1367
while proc .returncode is None :
1347
- mem_mb = max (mem_mb , _get_memory (proc .pid , include_children = True ))
1348
- num_threads = max (num_threads , _get_num_threads (psutil .Process (proc .pid )))
1349
- proc .poll ()
1368
+ mem_mb , num_threads = \
1369
+ _get_max_resources_used (proc , mem_mb , num_threads , poll = True )
1350
1370
proc .communicate ()
1351
1371
result ['stdout' ] = []
1352
1372
result ['stderr' ] = []
0 commit comments