I'm attempting to use Python subprocess.Popen
to run a batch file, which executes xfoil.exe and feeds it some parameters. Then it waits until xfoil.exe exits.
I've used the following code as a batch runner, which waits for the process to exit with a timeout. The process is killed if it times out.
def Bat_Runner(args, **kwargs):
"""
Batch runner
:param args:
:return:
"""
si = STARTUPINFO()
si.dwFlags |= STARTF_USESHOWWINDOW
process = Popen(args, stdin=PIPE, stdout=DEVNULL, bufsize=1, universal_newlines=True)
try:
process.wait(kwargs['timeout'])
except KeyError:
process.wait()
except TimeoutExpired:
process.kill()
raise TimeoutError
However, sometimes xfoil.exe fails and hangs, in which case the batch file also hangs, and the main program gets stuck at process.wait(kwargs['timeout])
. Even when the time limit is reached, the batch process is not killed. In this case, if I manually run the batch file by double clicking "XXX.bat", the console window also hangs.
What is the reason of this? I'm a bit puzzled regarding this since the Bat_Runner
function has been tested and works well at killing the process in some cases, but not this case.
Also, if kill()
is killing the batch process, is there any way to kill the xfoil.exe process that it invokes?
========================= Update Line ===============================
PS:
1 This Bat_Runner function will be called by this code:
def _solve_by_Xfoil(self, alpha_set, Mach_number, Raynolds_number, parameter, is_viscus=True, **kwargs):
...many unrelated codes...
try:
if kwargs.get('timeout'):
Bat_Runner(batch_file__bat, timout=kwargs['timeout'])
else:
Bat_Runner(batch_file__bat, timeout=5)
except TimeoutError:
print(''.join((Print_Colors.FAIL, 'Warning!\n', Print_Colors.ENDC,
'Current Airfoil {} Cannot get an Output: {}!')).format(self._name, parameter))
The codes are relatively big so for simplisity I just show what matters.Theoritically it will print an fail information. The _solve_by_Xfoil will be called by following:
cl_str_list = self._solve_by_Xfoil(alpha_range, Mach_number, Raynolds_number, 'CL', is_viscus=is_viscus,timeout=10, **kwargs)
The batch_file__bat(same as XXX.bat) is a file contain follows:
xfoil.exe < xfoil_batch_pid_11028.dat
2 For referrence the xfoil.exe is the this opensource program, I'm using its excutable exe file in this case, it will show a console window, just type some instructions to make it run, so I'm using batch file to run it.
3 Double click the batch file XXX.bat when xfoil.exe fails and hangs, the cmd window will stay and show following:
the batch file will run and disappear automatically if xfoil.exe run successfuly.
4 I'm prety sure that the batch file did not been killed because when I mannualy delete the xfoil_batch_pid_11028.dat which feeds to xfoil.exe when it fails, a message show that operation could not been completed cause this file has been open by windos cmd.