My script takes large number of files as input, calling external programs using subprocess.call(). It works perfectly from regular OS consoles, but hangs after processing 10 files or so when running in an embedded environment using its embedded Python 2.7.x.
I've referred to various similar questions and failed to find one working for me:
Python: subprocess.Popen and subprocess.call hang
and this in-depth discussion: https://thraxil.org/users/anders/posts/2008/03/13/Subprocess-Hanging-PIPE-is-your-enemy/
They all imply the vulnerable buffered PIPE, and suggest using file-like objects for stdout and stderr. So I also added a temporary text file, opened it, and fed it to stdout and stderr of subprocess.call(). It didn't work either.
My old code is fairly straight forward:
# script1.py
folder = "/path/to/my/folder"
for root, dirs, files in os.walk(folder):
for file in files:
path = join(root, file)
try:
cmd = ['dir', path, '1>&2']
_logger.debug(' '.join(cmd))
completed = subprocess.call(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as err:
_logger.debug(err)
else:
_logger.debug('returncode: {}'.format(completed))
print('all done!!')
the main script:
try:
cmd = ['python', 'script1.py', '1>&2']
ue.log(' '.join(cmd))
completed = subprocess.call(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as err:
_logger.error(err)
I've mixed-and-matched a couple of stdout/stderr solutions. including adding:
with open(join(_script_dir, 'tmp.txt'), 'w') as tmp:
#
# old code
#
completed = subprocess.call(cmd, shell=True, stdout=tmp, stderr=tmp)
None worked.