Skip to content

Commit e5945e9

Browse files
committed
Changed resources fetching to its function and try-blocked it in case of dying processes
1 parent 08a485d commit e5945e9

File tree

1 file changed

+33
-13
lines changed

1 file changed

+33
-13
lines changed

nipype/interfaces/base.py

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1204,6 +1204,7 @@ def _read(self, drain):
12041204
self._lastidx = len(self._rows)
12051205

12061206

1207+
# Get number of threads for process
12071208
def _get_num_threads(proc):
12081209
'''
12091210
'''
@@ -1223,6 +1224,29 @@ def _get_num_threads(proc):
12231224
return num_threads
12241225

12251226

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+
12261250
def run_command(runtime, output=None, timeout=0.01, redirect_x=False):
12271251
"""Run a command, read stdout and stderr, prefix with timestamp.
12281252
@@ -1231,7 +1255,7 @@ def run_command(runtime, output=None, timeout=0.01, redirect_x=False):
12311255

12321256
# Import packages
12331257
try:
1234-
from memory_profiler import _get_memory
1258+
import memory_profiler
12351259
import psutil
12361260
mem_prof = True
12371261
except:
@@ -1273,7 +1297,6 @@ def run_command(runtime, output=None, timeout=0.01, redirect_x=False):
12731297
# Init variables for memory profiling
12741298
mem_mb = -1
12751299
num_threads = -1
1276-
interval = 1
12771300

12781301
if output == 'stream':
12791302
streams = [Stream('stdout', proc.stdout), Stream('stderr', proc.stderr)]
@@ -1292,8 +1315,8 @@ def _process(drain=0):
12921315
stream.read(drain)
12931316
while proc.returncode is None:
12941317
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)
12971320
proc.poll()
12981321
_process()
12991322
_process(drain=1)
@@ -1311,9 +1334,8 @@ def _process(drain=0):
13111334
if output == 'allatonce':
13121335
if mem_prof:
13131336
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)
13171339
stdout, stderr = proc.communicate()
13181340
if stdout and isinstance(stdout, bytes):
13191341
try:
@@ -1332,9 +1354,8 @@ def _process(drain=0):
13321354
if output == 'file':
13331355
if mem_prof:
13341356
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)
13381359
ret_code = proc.wait()
13391360
stderr.flush()
13401361
stdout.flush()
@@ -1344,9 +1365,8 @@ def _process(drain=0):
13441365
if output == 'none':
13451366
if mem_prof:
13461367
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)
13501370
proc.communicate()
13511371
result['stdout'] = []
13521372
result['stderr'] = []

0 commit comments

Comments
 (0)