首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在Windows中处理子进程崩溃

在Windows中处理子进程崩溃
EN

Stack Overflow用户
提问于 2011-02-22 01:33:42
回答 2查看 8.6K关注 0票数 21

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

代码语言:javascript
复制
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总是在某个MP3s上崩溃(并且名副其实)。Windows的“您的程序已崩溃”对话框出现,冻结我的脚本。关闭窗口对话框后,将引发AudioProcessingException。我只想让脚本抛出异常,然后转到下一个MP3,而不是让Windows闭嘴。

有什么办法可以解决这个问题吗?优选地,通过改变脚本而不是用Unix运行它。

我使用的是Windows 7和Python 2.6

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-02-24 19:21:08

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

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

代码语言:javascript
复制
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
票数 23
EN

Stack Overflow用户

发布于 2019-07-01 22:32:38

我对creationflags=subprocess.CREATE_NO_WINDOW很感兴趣。即使应用程序崩溃,也会返回正确的应用程序输出。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5069224

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档