Skip to content

[ENH] Revising use of subprocess.Popen #2289

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Nov 28, 2017

Conversation

oesteban
Copy link
Contributor

This PR:

  • Revises the use of Popen in CommandLineInterface ensuring they are terminated and garbage-collected.
  • Tidy up a bit the code:
    • Removes unused code for getting the version of the command (interfaces.base)
    • Make sure the ordering of imports is okay (interfaces.base)
    • Comment out historical hacks that potentially can be removed (interfaces.base)
    • Minor modifications in engine.workflows and plugins.multiproc.
  • Simplifies get_dependencies and executes it only iff the option execution. get_linked_libs is True (which is the default value).

Copy link
Member

@effigies effigies left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if this is ready for comment, but a couple comments anyway. Assume the commented-out bits will be uncommented or deleted before merge?

stderr=sp.PIPE,
)
o, e = proc.communicate()
return o
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an API change. Even if not used internally, someone may depend on this method. Does nipype have a process for deprecating?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should start using https://pypi.python.org/pypi/deprecation ?


runtime.returncode = proc.returncode
proc.terminate() # Ensure we are done
gc.collect() # Force GC for a cleanup
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Goal here is just to clean up proc internals, not stdout/stderr, etc?

proc = sp.Popen(
cmd(name), stdout=sp.PIPE, stderr=sp.PIPE, shell=True,
env=environ, close_fds=True)
o, e = proc.communicate()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd replace e with _, since it's unused.

@@ -1452,12 +1448,28 @@ def _process(drain=0):
result['merged'] = result['stdout']
result['stdout'] = []
else:
proc.communicate() # Discard stdout and stderr
stdout, stderr = proc.communicate()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe make these stdoutstr, stdinstr, to be consistent with if output.startswith('file'), so that the same del statement will work in all cases.

Alternately, it might be simplest to just run gc.collect() in CommandLine._run_interface, after this function is called and everything is allowed to go out of scope.

result['stdout'] = read_stream(stdoutstr, logger=iflogger)
result['stderr'] = read_stream(stderrstr, logger=iflogger)
del stdoutstr
del stderrstr
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, meant for these to replace the del stdout and del stderr below, since stdoutstr and stderrstr can be created in the other branch.

I guess, though, that those only exist under certain sub-branches. Not sure how you want to go about this.

@oesteban oesteban added this to the 0.14.0 milestone Nov 22, 2017
_ = getattr(dup, key)
except:
pass
# for key in self.copyable_trait_names():
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dynamic traits won't work without these. i'm not sure we have a test for it but this is the relevant issue and PR.

enthought/traits#373

@@ -1620,6 +1642,8 @@ def _get_environ(self):
return getattr(self.inputs, 'environ', {})

def version_from_command(self, flag='-v'):
iflogger.warning('version_from_command member of CommandLine was '
'Deprecated in nipype-1.0.0 and deleted in 2.0.0')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should perhaps keep this in the 1.0 branch so deleted in 1.1

@mgxd mgxd merged commit 10a0a41 into nipy:master Nov 28, 2017
@oesteban oesteban deleted the enh/revising-popen-calls branch November 28, 2017 18:57
oesteban added a commit to oesteban/fmriprep that referenced this pull request Nov 29, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants