Repository: nipy/nipype

Issue:
NoSuchProcess in short-lived nodes (resouce monitoring)
```
multiprocessing.pool.RemoteTraceback: 
"""
Traceback (most recent call last):
  File "/usr/local/miniconda/lib/python3.6/multiprocessing/pool.py", line 119, in worker
    result = (True, func(*args, **kwds))
  File "/usr/local/miniconda/lib/python3.6/multiprocessing/pool.py", line 44, in mapstar
    return list(map(*args))
  File "/usr/local/miniconda/lib/python3.6/site-packages/fmriprep/interfaces/itk.py", line 237, in _mat2itk
    fsl2ras=True, itk_transform=out_file).run()
  File "/usr/local/miniconda/lib/python3.6/site-packages/niworkflows/nipype/interfaces/base.py", line 1080, in run
    mon_sp = ResourceMonitor(proc_pid, freq=mon_freq)
  File "/usr/local/miniconda/lib/python3.6/site-packages/niworkflows/nipype/utils/profiler.py", line 50, in __init__
    self._sample(cpu_interval=0.2)
  File "/usr/local/miniconda/lib/python3.6/site-packages/niworkflows/nipype/utils/profiler.py", line 82, in _sample
    for child in self._process.children(recursive=True):
  File "/usr/local/miniconda/lib/python3.6/site-packages/psutil/__init__.py", line 362, in wrapper
    raise NoSuchProcess(self.pid, self._name)
psutil.NoSuchProcess: psutil.NoSuchProcess process no longer exists (pid=11094)
"""

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/local/miniconda/lib/python3.6/site-packages/niworkflows/nipype/pipeline/plugins/multiproc.py", line 51, in run_node
    result['result'] = node.run(updatehash=updatehash)
  File "/usr/local/miniconda/lib/python3.6/site-packages/niworkflows/nipype/pipeline/engine/nodes.py", line 407, in run
    self._run_interface()
  File "/usr/local/miniconda/lib/python3.6/site-packages/niworkflows/nipype/pipeline/engine/nodes.py", line 517, in _run_interface
    self._result = self._run_command(execute)
  File "/usr/local/miniconda/lib/python3.6/site-packages/niworkflows/nipype/pipeline/engine/nodes.py", line 650, in _run_command
    result = self._interface.run()
  File "/usr/local/miniconda/lib/python3.6/site-packages/niworkflows/nipype/interfaces/base.py", line 1088, in run
    runtime = self._run_interface(runtime)
  File "/usr/local/miniconda/lib/python3.6/site-packages/fmriprep/interfaces/itk.py", line 62, in _run_interface
    for i, in_mat in enumerate(self.inputs.in_files)]
  File "/usr/local/miniconda/lib/python3.6/multiprocessing/pool.py", line 260, in map
    return self._map_async(func, iterable, mapstar, chunksize).get()
  File "/usr/local/miniconda/lib/python3.6/multiprocessing/pool.py", line 608, in get
    raise self._value
psutil.NoSuchProcess: psutil.NoSuchProcess process no longer exists (pid=11094)

```


Discussion:

Resolving patch:
diff --git a/nipype/utils/profiler.py b/nipype/utils/profiler.py
--- a/nipype/utils/profiler.py
+++ b/nipype/utils/profiler.py
@@ -2,7 +2,7 @@
 # @Author: oesteban
 # @Date:   2017-09-21 15:50:37
 # @Last Modified by:   oesteban
-# @Last Modified time: 2017-10-02 15:44:29
+# @Last Modified time: 2017-10-20 09:12:36
 """
 Utilities to keep track of performance
 """
@@ -77,9 +77,13 @@ def _sample(self, cpu_interval=None):
         except psutil.NoSuchProcess:
             pass
 
-        # parent_mem = mem
         # Iterate through child processes and get number of their threads
-        for child in self._process.children(recursive=True):
+        try:
+            children = self._process.children(recursive=True)
+        except psutil.NoSuchProcess:
+            children = []
+
+        for child in children:
             try:
                 with child.oneshot():
                     cpu += child.cpu_percent()


Test patch: