IMP logo
IMP Reference Guide  develop.7482b7c7af, 2026/12/05
The Integrative Modeling Platform
subproc.py
1"""@namespace IMP.parallel.subproc Subprocess handling."""
2
3import sys
4import subprocess
5
6
7class _Popen4(subprocess.Popen):
8
9 """Utility class to provide a portable way to spawn a child process and
10 communicate with its stdin and combined stdout/stderr."""
11
12 def __init__(self, cmd):
13 # shell isn't needed on Win32, and may not be found under wine anyway
14 shell = (sys.platform != "win32")
15 subprocess.Popen.__init__(
16 self, cmd, shell=shell, stdin=subprocess.PIPE,
17 stdout=subprocess.PIPE,
18 stderr=subprocess.STDOUT, universal_newlines=True)
19
20 def require_clean_exit(self):
21 """Make sure the child exited with a zero return code"""
22 r = self.wait()
23 if r != 0:
24 raise IOError("Process failed with exit status %d" % r)
25
26
27if sys.platform == 'win32':
28 def _run_background(cmdline, out):
29 """Run a process in the background and direct its output to a file"""
30 print("%s > %s" % (cmdline, out))
31 try:
32 # shell isn't needed on Win32, and may not be found under wine
33 # anyway
34 _ = subprocess.Popen(cmdline, shell=False, stdout=open(out, 'w'),
35 stderr=subprocess.STDOUT,
36 universal_newlines=True)
37 # Ignore Windows "file not found" errors, so that behavior is
38 # consistent between Unix and Windows
39 except WindowsError as detail:
40 print(("WindowsError: %s (ignored)" % detail))
41
42else:
43
44 def _run_background(cmdline, out):
45 """Run a process in the background and direct its output to a file"""
46 print("%s > %s" % (cmdline, out))
47 subprocess.Popen(cmdline, shell=True, stdout=open(out, 'w'),
48 stderr=subprocess.STDOUT,
49 universal_newlines=True)