Handling subprocess crash in Windows

后端 未结 2 417
刺人心
刺人心 2020-12-05 00:40

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_         


        
相关标签:
2条回答
  • 2020-12-05 01:40

    Works for me with creationflags=subprocess.CREATE_NO_WINDOW. Even if the application crashes the correct application output will be returned.

    0 讨论(0)
  • 2020-12-05 01:41

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

    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
    
    0 讨论(0)
提交回复
热议问题