0

From a Python script I would like to open vlc in a new thread and allow the user to close it cleanly (still from this script). It appears that the send_signal() instruction does not actually close vlc, what am I doing wrong?

import subprocess
import signal

s = subprocess.Popen("vlc", shell=True)
raw_input("Press Enter to stop vlc...")
s.send_signal(signal.SIGINT)
print "waiting for vlc to exit..."
s.wait()

EDIT: I replaced vlc for testing/illustrating purposes but my real need is ending a stream being recorded by ffmpeg, which is normally listening to SIGINT since this is the standard signal to exit (it says "Press ctrl-c to stop encoding").

myoan
  • 401
  • 5
  • 14

3 Answers3

1

SIGINT is normally reserved for Ctrl-C interruption. It is likely to be ignored in a GUI application. The correct signal to send to gracefully terminate an application is SIGTERM, or SIGKILL to terminate it abruptly.

But with the subprocess module, you can (should) directly call the methods terminate or kill of the subprocess object.

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
  • I can confirm that neither kill() nor terminate() close vlc with the above code under Linux and that vlc is actually closed when it received a SIGINT (tested with kill -2 ) – myoan Dec 11 '14 at 13:16
0

It is likely that vlc is ignoring the signal. Try sending SIGTERM or SIGKILL.

See this link for more details on Unix termination signals (assuming you are running under Unix/Linux.) See the notes on which signals can be blocked and/or ignored.

ErikR
  • 51,541
  • 9
  • 73
  • 124
0

Ok I finally short-cut the shell with

s = subprocess.Popen(["vlc"])  

And sending SIGINT/SIGKILL now actually closes the process. I have no explanation why the first option using the shell does not work however.

myoan
  • 401
  • 5
  • 14
  • 2
    it's because the subprocess you're hooked up to when `shell=True` is the shell, not the process you wanted the shell to open, and the shell ignores SIGINT while it has children still running (in this case, "vlc") instead of passing them to your intended target. – KingRadical Jun 05 '15 at 22:47