5

私は、1日に約20,000回独自のアプリケーション(時々クラッシュする)を実行する必要があるpythonアプリケーションを持っています。

問題は、アプリケーションがクラッシュすると、Windows が自動的にWerFaultをトリガーし、プログラムがハングしたままになるため、Pythonsubprocess.call()はユーザー入力を永遠に待機します (そのアプリケーションは週末、休日、24 時間年中無休で実行する必要があるため、これは受け入れられません)。

使用してもsleep; poll; kill; terminate使用できなくなる場合communicate()、アプリケーションは数ミリ秒から 2 時間実行できるため、固定タイムアウトを設定しても効果がありません。

また、自動デバッグをオンにしようとしました (アプリケーションのクラッシュ ダンプを取得して ID を終了するスクリプトを使用します) が、どういうわけかこのハウツーがサーバーで機能しません (WerFault がまだ表示され、ユーザー入力を待機します)

このような他のいくつかのチュートリアルも効果がありませんでした.

質問: WerFault が表示されないようにする (ユーザー入力を待機する) 方法はありますか? これはプログラミングよりもシステムに関する質問です

別の質問: Python でアプリケーションのクラッシュを検出する適切な方法はありますか ( WerFaultが表示されたかどうか)

4

2 に答える 2

2

シンプルな(そして醜い)答え、WerFault.exe特に問題のあるアプリケーションに関連するインスタンスを時々監視しますPID。そして、それを殺します。対処WerFault.exeは複雑ですが、無効にしたくありません。Windows エラー報告サービスを参照してください。

  1. 一致するプロセスのリストを名前で取得しますWerFault.exepsutilパッケージを使用しています。psutilプロセスはキャッシュされるので注意して使用してpsutil.get_pid_list()ください。
  2. を使用してコマンド ラインをデコードしargparseます。これはやり過ぎかもしれませんが、既存の Python ライブラリを活用します。
  3. に従って、アプリケーションを保持しているプロセスを特定します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関数はtaskkill正しいコマンドでコマンドを呼び出しますPID

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())
于 2013-07-29T23:07:53.090 に答える
-2

プログラムがクラッシュし、問題のあるコードを見つけて、それを try ステートメントに入れる必要がある理由がわかりません。

http://docs.python.org/3.2/tutorial/errors.html#handling-exceptions

于 2012-12-19T17:27:35.547 に答える