@@ -1204,6 +1204,25 @@ def _read(self, drain):
1204
1204
self ._lastidx = len (self ._rows )
1205
1205
1206
1206
1207
+ def _get_num_threads (proc ):
1208
+ '''
1209
+ '''
1210
+
1211
+ # Import packages
1212
+ import psutil
1213
+
1214
+ # Init variables
1215
+ num_threads = proc .num_threads ()
1216
+ try :
1217
+ for child in proc .children ():
1218
+ num_threads = max (num_threads , child .num_threads (),
1219
+ len (child .children ()), _get_num_threads (child ))
1220
+ except psutil .NoSuchProcess :
1221
+ dummy = 1
1222
+
1223
+ return num_threads
1224
+
1225
+
1207
1226
def run_command (runtime , output = None , timeout = 0.01 , redirect_x = False ):
1208
1227
"""Run a command, read stdout and stderr, prefix with timestamp.
1209
1228
@@ -1213,6 +1232,7 @@ def run_command(runtime, output=None, timeout=0.01, redirect_x=False):
1213
1232
# Import packages
1214
1233
try :
1215
1234
from memory_profiler import _get_memory
1235
+ import psutil
1216
1236
mem_prof = True
1217
1237
except :
1218
1238
mem_prof = False
@@ -1253,7 +1273,7 @@ def run_command(runtime, output=None, timeout=0.01, redirect_x=False):
1253
1273
# Init variables for memory profiling
1254
1274
mem_mb = - 1
1255
1275
num_threads = - 1
1256
- interval = 0. 1
1276
+ interval = 1
1257
1277
1258
1278
if output == 'stream' :
1259
1279
streams = [Stream ('stdout' , proc .stdout ), Stream ('stderr' , proc .stderr )]
@@ -1273,8 +1293,7 @@ def _process(drain=0):
1273
1293
while proc .returncode is None :
1274
1294
if mem_prof :
1275
1295
mem_mb = max (mem_mb , _get_memory (proc .pid , include_children = True ))
1276
- num_threads = max (num_threads , psutil .Process (proc .pid ).num_threads ())
1277
- time .sleep (interval )
1296
+ num_threads = max (num_threads , _get_num_threads (psutil .Process (proc .pid )))
1278
1297
proc .poll ()
1279
1298
_process ()
1280
1299
_process (drain = 1 )
@@ -1293,8 +1312,7 @@ def _process(drain=0):
1293
1312
if mem_prof :
1294
1313
while proc .returncode is None :
1295
1314
mem_mb = max (mem_mb , _get_memory (proc .pid , include_children = True ))
1296
- num_threads = max (num_threads , psutil .Process (proc .pid ).num_threads ())
1297
- time .sleep (interval )
1315
+ num_threads = max (num_threads , _get_num_threads (psutil .Process (proc .pid )))
1298
1316
proc .poll ()
1299
1317
stdout , stderr = proc .communicate ()
1300
1318
if stdout and isinstance (stdout , bytes ):
@@ -1315,8 +1333,7 @@ def _process(drain=0):
1315
1333
if mem_prof :
1316
1334
while proc .returncode is None :
1317
1335
mem_mb = max (mem_mb , _get_memory (proc .pid , include_children = True ))
1318
- num_threads = max (num_threads , psutil .Process (proc .pid ).num_threads ())
1319
- time .sleep (interval )
1336
+ num_threads = max (num_threads , _get_num_threads (psutil .Process (proc .pid )))
1320
1337
proc .poll ()
1321
1338
ret_code = proc .wait ()
1322
1339
stderr .flush ()
@@ -1328,8 +1345,7 @@ def _process(drain=0):
1328
1345
if mem_prof :
1329
1346
while proc .returncode is None :
1330
1347
mem_mb = max (mem_mb , _get_memory (proc .pid , include_children = True ))
1331
- num_threads = max (num_threads , psutil .Process (proc .pid ).num_threads ())
1332
- time .sleep (interval )
1348
+ num_threads = max (num_threads , _get_num_threads (psutil .Process (proc .pid )))
1333
1349
proc .poll ()
1334
1350
proc .communicate ()
1335
1351
result ['stdout' ] = []
0 commit comments