[英]Kill python script launched by a subprocess called by another script (Windows)
I'm trying to integrate ESA'2 sen2cor python-script into my workflow.我正在尝试将 ESA'2 sen2cor python-script 集成到我的工作流程中。 To do this I create a subprocess with which I call the "L2A_Process.bat" file, which in turn calls the "L2A_Process.py" script.为此,我创建了一个名为“L2A_Process.bat”文件的子进程,该文件又调用“L2A_Process.py”脚本。 I want to launch the sen2cor-script with a timeout since it gets stuck and freezes from time to time, so as to automatically re-launch it if it failed.我想在超时的情况下启动 sen2cor-script,因为它会不时卡住并冻结,以便在失败时自动重新启动它。 To launch it and catch a timeout I successfully used the following script:为了启动它并捕获超时,我成功地使用了以下脚本:
import os, subprocess
from signal import CTRL_BREAK_EVENT
timeout = 3600 #1hour
l1c_safe_path = "path/to/my/input/file.SAFE" #product that I want to process
command = ["L2A_process.bat", l1c_safe_path]
p = subprocess.Popen(command,shell=False, creationflags=subprocess.CREATE_NEW_PROCESS_GROUP)
try:
p.wait(timeout)
except subprocess.TimeoutExpired:
os.kill(p.pid, CTRL_BREAK_EVENT)
This is as far as I got.这是我得到的。 It results in the sen2cor-script being paused giving the following output:它会导致 sen2cor 脚本暂停,并给出以下 output:
Terminate batch job (Y/N)
I'd like to know how I can properly terminate my subprocess "p" with all it own child-subprocesses (ie "L2A_Process.py").我想知道如何使用它自己的所有子子进程(即“L2A_Process.py”)正确终止我的子进程“p”。
Some observations:一些观察:
This script needs to run on Windows;此脚本需要在 Windows 上运行;
I've tried to kill the subprocess without the creationflag I've used in the example above: this results in the subprocess being killed but the "L2A_Process.py" script deteaches an keeps running (which is the core of my problem);我试图在没有上例中使用的创建标志的情况下终止子进程:这会导致子进程被终止,但“L2A_Process.py”脚本会取消持续运行(这是我的问题的核心);
This code works for me to monitor Sen2cor status while converting L1C to L2A for Sentinel 2 data.此代码适用于我监控 Sen2cor 状态,同时将 L1C 转换为 L2A 以获取 Sentinel 2 数据。 The Sen2cor process is time and cpu consuming so be patient. Sen2cor 过程是时间和 CPU 消耗,所以请耐心等待。 It took half an hour to create L2A with DEM, DDV, etc. Hope it helps用DEM、DDV等创建L2A花了半个小时,希望对你有帮助
from subprocess import Popen, PIPE
import os
pathtoprodS1C = "path_to_L1C_product" // safe file
outdirS2A = "output_dir" // where L2A files will be placed
pathtoL2Process = "path_to_L2A_Process" //if not in path
pathtoGIPP = "path_to_your_GIPP/L2A_GIPP.xml"
procName = "./L2A_Process"
os.chdir(pathtoL2Process)
import shlex
pcall = "./{} {} --output_dir {} --tif --GIP_L2A {}".format(procName,
pathtoprodS1C,
pathtoprodS2A,
pathtoGIPP)
args = shlex.split(pcall)
print(args)
try:
p = Popen(args, stdout=PIPE)
eut = p.stdout.readline()
while eut:
eut = p.stdout.readline()
print(eut)
finally:
print('Sen2Cor is Done')
exit()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.