Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Windows 如何在子流程中启动崩溃(很少)应用程序_Windows_Python 3.x_Crash_Subprocess - Fatal编程技术网

Windows 如何在子流程中启动崩溃(很少)应用程序

Windows 如何在子流程中启动崩溃(很少)应用程序,windows,python-3.x,crash,subprocess,Windows,Python 3.x,Crash,Subprocess,我有一个python应用程序,它需要每天执行大约20000次专有应用程序(有时会崩溃) 问题是,当应用程序崩溃时,Windows会自动触发,这将使程序保持挂起状态,因此将永远等待用户输入(该应用程序必须在周末、节假日、24/7运行…因此这是不可接受的) 如果想使用睡眠;投票杀戮;终止,但这将意味着失去使用通信()的能力,应用程序可以从几毫秒运行到2小时,因此设置固定超时将无效 我也尝试过(使用一个脚本,它将获取应用程序的崩溃转储并终止id),但不知何故,这个howto在我的服务器上不起作用(We

我有一个python应用程序,它需要每天执行大约20000次专有应用程序(有时会崩溃)

问题是,当应用程序崩溃时,Windows会自动触发,这将使程序保持挂起状态,因此将永远等待用户输入(该应用程序必须在周末、节假日、24/7运行…因此这是不可接受的)

如果想使用
睡眠;投票杀戮;终止
,但这将意味着失去使用
通信()的能力
,应用程序可以从几毫秒运行到2小时,因此设置固定超时将无效

我也尝试过(使用一个脚本,它将获取应用程序的崩溃转储并终止id),但不知何故,这个howto在我的服务器上不起作用(WerFault仍然出现并等待用户输入)

其他一些教程也没有起到任何作用

问题: 是否有办法防止WerFault显示(等待用户输入)?这是一个比编程更复杂的系统问题


备选问题:python中是否有一种优雅的方法来检测应用程序崩溃(是否显示了WerFault)

我看不出程序需要崩溃的原因,找到有问题的代码片段,并将其放入try语句中

简单(丑陋)的回答,不时监视
WerFault.exe
实例,特别是与违规应用程序的
PID
相关的实例。然后杀了它。处理
WerFault.exe
很复杂,但您不想禁用它——请参阅服务

  • 按名称获取与
    WerFault.exe
    匹配的进程列表。我使用软件包。小心使用
    psutil
    ,因为进程是缓存的,所以请使用
    psutil.get\u pid\u list()
  • 使用
    argparse
    对其命令行进行解码。这可能有些过分,但它利用了现有的python库
  • 根据应用程序的
    PID
    标识保存应用程序的进程
  • 这是一个简单的实现

    def kill_proc_kidnapper(self, child_pid, kidnapper_name='WerFault.exe'):
        """
        Look among all instances of 'WerFault.exe' process for an specific one
        that took control of another faulting process.
        When 'WerFault.exe' is launched it is specified the PID using -p argument:
    
        'C:\\Windows\\SysWOW64\\WerFault.exe -u -p 5012 -s 68'
                                 |               |
                                 +-> kidnapper   +-> child_pid
    
        Function uses `argparse` to properly decode process command line and get
        PID. If PID matches `child_pid` then we have found the correct parent
        process and can kill it.
        """
        parser = argparse.ArgumentParser()
        parser.add_argument('-u', action='store_false', help='User name')
        parser.add_argument('-p', type=int, help='Process ID')
        parser.add_argument('-s', help='??')
    
        kidnapper_p = None
        child_p = None
    
        for proc in psutil.get_pid_list():
            if kidnapper_name in proc.name:
                args, unknown_args = parser.parse_known_args(proc.cmdline)
                print proc.name, proc.cmdline
    
                if args.p == child_pid:
                    # We found the kidnapper, aim.
                    print 'kidnapper found: {0}'.format(proc.pid)
                    kidnapper_p = proc
    
        if psutil.pid_exists(child_pid):
            child_p = psutil.Process(child_pid)
    
        if kidnapper_p and child_pid:
            print 'Killing "{0}" ({1}) that kidnapped "{2}" ({3})'.format(
                kidnapper_p.name, kidnapper_p.pid, child_p.name, child_p.pid)
            self.taskkill(kidnapper_p.pid)
            return 1
        else:
            if not kidnapper_p:
                print 'Kidnapper process "{0}" not found'.format(kidnapper_name)
            if not child_p:
                print 'Child process "({0})" not found'.format(child_pid)
    
        return 0
    
    现在,
    taskkill
    函数使用正确的
    PID
    调用
    taskkill
    commmand

    def taskkill(self, pid):
        """
        Kill task and entire process tree for this process
        """
        print('Task kill for PID {0}'.format(pid))
        cmd = 'taskkill /f /t /pid {0}'.format(pid)
        subprocess.call(cmd.split())
    

    您可以将崩溃应用程序添加到wer排除列表中,然后依赖本地转储进行崩溃转储。看看你有没有看到问题是专有应用程序正在崩溃。我们对此无能为力(支持差):(应用程序崩溃。这是生活中的一个事实。即使是你的应用程序,你可以修复崩溃,处理崩溃通常是有意义的。在这种情况下,第三方应用程序无论如何都无法修复。非常好的第一个答案:)我基本上已经在做类似的事情了,但我并没有终止进程,而是通过nagios通知管理员,这样他们就可以创建崩溃转储。