简体   繁体   English

杀死由另一个脚本调用的子进程启动的 python 脚本(Windows)

[英]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”脚本会取消持续运行(这是我的问题的核心);

  • I cannot use a CTRL_C_EVENT since I want to re-launch the failed "L2A_Process.py" in a loop until it succeeds.我不能使用 CTRL_C_EVENT,因为我想在循环中重新启动失败的“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.

相关问题 Python杀死父脚本(子进程) - Python kill parent script (subprocess) Python脚本使用子进程获取PID并杀死它,而从其坐目录外启动时,行为异常 - Python script using subprocess to get PID and kill it acts weird when launched from outside its sitting directory 从另一个脚本中杀死一个python脚本-CalledProcessError - Kill a python script from another script - CalledProcessError 如何使用Python CGI脚本杀死启动该脚本的浏览器? - How can I use a Python cgi script to kill the browser that launched it? 从另一个Python脚本调用的Python脚本作为子进程执行.jar文件时出错 - Error in executing .jar file from a Python script called from another Python script, as a subprocess 有没有办法等待从当前脚本(使用 subprocess.Propen())调用的另一个 python 脚本直到它完成? - Is there a way to wait for another python script called from current script (using subprocess.Propen()) till its complete? 确定是否将python脚本称为子​​进程并传递args - Detemine if python script is called as subprocess and passing args 运行子进程后如何立即杀死Python脚本? - How to immediately kill Python script after running subprocess? Python 2.7.9在运行脚本时无法终止子进程 - Python 2.7.9 Can not kill subprocess while running script 如何在windows中杀死子进程python - How to kill subprocess python in windows
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM