【问题标题】:Handling subprocess crash in Windows处理 Windows 中的子进程崩溃
【发布时间】:2011-07-01 10:50:50
【问题描述】:

我正在从 Windows 命令提示符运行 python 脚本。它调用下面的函数,使用LAME将MP3文件转换为波形文件。

def convert_mp3_to_wav(input_filename, output_filename):
    """
    converts the incoming mp3 file to wave file
    """
    if not os.path.exists(input_filename):
        raise AudioProcessingException, "file %s does not exist" % input_filename

    command = ["lame", "--silent", "--decode", input_filename, output_filename]

    process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    (stdout, stderr) = process.communicate()

    if process.returncode != 0 or not os.path.exists(output_filename):
        raise AudioProcessingException, stdout

    return output_filename

不幸的是,LAME 总是在某些 MP3 上崩溃(并且名副其实)。 Windows“您的程序已崩溃”对话框出现,它冻结了我的脚本。一旦我关闭 Windows 对话框,就会引发 AudioProcessingException。 不必告诉 Windows 关闭,我只想让脚本引发异常,然后转到下一个 MP3。

有没有办法解决这个问题?最好通过修改脚本而不是在 Unix 上运行它。

我使用的是 Windows 7 和 Python 2.6

【问题讨论】:

  • +1 表示“最好通过修改脚本而不是在 Unix 上运行它。”
  • 您是否尝试过在 Windows 中禁用错误​​报告?
  • 呃不,我没有……我会调查的!
  • 我关闭了 Windows 错误报告,但我仍然收到停止脚本运行的通知。我不确定关闭 Windows 的更多部分是否会让我的系统管理员高兴!
  • 这很棘手,我找不到任何有关自己解决此问题的信息。出于兴趣,您是否尝试过使用 .wait() 而不是 .communicate()?我意识到这会锁定您的代码,直到编码完成,或者可能无法解决任何问题。只是一个测试。

标签: python subprocess


【解决方案1】:

在谷歌搜索之后,我偶然发现了这个 http://www.activestate.com/blog/2007/11/supressing-windows-error-report-messagebox-subprocess-and-ctypes

它需要一些修修补补,但下面的方法现在不会收到烦人的 Windows 消息 :) 注意 subprocess.Popen 中的 creationflags=subprocess_flags

def convert_mp3_to_wav(input_filename, output_filename):

    if sys.platform.startswith("win"):
        # Don't display the Windows GPF dialog if the invoked program dies.
        # See comp.os.ms-windows.programmer.win32
        # How to suppress crash notification dialog?, Jan 14,2004 -
        # Raymond Chen's response [1]

        import ctypes
        SEM_NOGPFAULTERRORBOX = 0x0002 # From MSDN
        ctypes.windll.kernel32.SetErrorMode(SEM_NOGPFAULTERRORBOX);
        subprocess_flags = 0x8000000 #win32con.CREATE_NO_WINDOW?
    else:
        subprocess_flags = 0



    """
    converts the incoming mp3 file to wave file
    """
    if not os.path.exists(input_filename):
        raise AudioProcessingException, "file %s does not exist" % input_filename

    #exec("lame {$tmpname}_o.mp3 -f {$tmpname}.mp3 && lame --decode {$tmpname}.mp3 {$tmpname}.wav");
    command = ["lame", "--silent", "--decode", input_filename, output_filename]

    process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, creationflags=subprocess_flags)
    (stdout, stderr) = process.communicate()

    if process.returncode != 0 or not os.path.exists(output_filename):
        raise AudioProcessingException, stdout

    return output_filename

【讨论】:

  • 很抱歉 3 年后劫持了这个答案,但我需要为我尝试执行的 Python 脚本禁用 Windows 错误报告,但我没有使用子进程,而是线程。我添加了您的第一个代码“段落”,现在错误没有出现,但命令行中没有显示有关崩溃的信息。这正常吗?
  • @Shadark 我不记得对这些错误做过任何事情,所以我怀疑我当时是否关心缺乏信息。正如你所说,已经3年了……我现在没有Windows机器,所以我真的帮不上忙!对不起。
【解决方案2】:

creationflags=subprocess.CREATE_NO_WINDOW 为我工作。即使应用程序崩溃,也会返回正确的应用程序输出。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-16
    • 1970-01-01
    相关资源
    最近更新 更多