本文介绍了处理 Windows 中的子进程崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am running a python script from windows command prompt. It calls the function below, which converts an MP3 file to a wave file using LAME.

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.

Unfortunately LAME always crashes (and lives up to its name) on a certain MP3s. The Windows "Your program has crashed" dialog appears, which freezes my script. Once I close the windows dialog, the AudioProcessingException is raised.Instead of having to tell Windows to shut up, I'd just like the script to raise the exception and then move onto the next MP3.

有什么办法可以解决这个问题吗?最好通过更改脚本而不是在 Unix 上运行它.

Is there any way around this? Preferably by altering the script rather than running it with Unix.

我使用的是 Windows 7 和 Python 2.6

I am using Windows 7, and Python 2.6

推荐答案

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

After some more googling, I stumbled upon thishttp://www.activestate.com/blog/2007/11/supressing-windows-error-report-messagebox-subprocess-and-ctypes

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

It required a bit of tinkering, but the method below now doesn't get annoying Windows messages :)Note the creationflags=subprocess_flags in the subprocess.Popen too

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

这篇关于处理 Windows 中的子进程崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 05:44