简体   繁体   中英

How to effectively kill a frozen subprocess in Python?

I am dealing with a rather odd issue.

I've written a Python wrapper (in Python 2.6.6) for a year old version of ffmpeg. The problem is that given a very particular video, ffmpeg stops working normally (clunky output, full cpu usage, no end stage etc) and takes the python interpreter down with it.

Now, if I run ffmpeg with my encoding options directly from a terminal and the problematic video as input, ffmpeg won't immediately respond to Ctrl-c . I'll have to wait for a hefty of 10 seconds or more before it exits and gives me back the prompt. However if I use a 'healthy' video instead, it will simply print Received signal 2: terminating. and gracefully exit.

In the python wrapper I use p.kill() to no effect. The behavior is exactly the same aka I have to wait 10 sec before the program exits. How can I immediately stop ffmpeg when it freezes with some problematic video?

Note that if I do a double Ctrl-c I get the prompt back immediately no matter what.

How are you executing the FFmpeg subprocess? There are lots of ways to launch and monitor processes in Python.

I used to deal with this exact problem because I used to run continuous automated testing for the FFmpeg project. When I needed to debug this, I would print out the PID that my program thinks corresponds to the running FFmpeg process. Then I would use top or ps to verify that the PID was correct. There was some weirdness between Linux and Mac OS X concerning shell parameters passed to the Python function and whether the PID correlated to the FFmpeg process or to the shell that launched the FFmpeg process.

See also: this old blog post I wrote exploring the problem, and the numerous responses .

如果您使用的是Linux,则可以通过以下方式将其留在shell中:

subprocess.call( "kill -9 $(pidof ffmpeg)", shell=True )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM