Check for status of subprocess to kill audio playback in python
<p>Im currently using sox to apply effects to an audio file and play the file back. Currently I have the code set to create a new subprocess to play the file when button 1 is pressed and to kill the process when button 2 is pressed. </p>

<p>What I want to do is change it so that if Button 1 is pressed multiple times it will check to see if a previous process is active and if so kill it and if not then just play as normal.</p>

<p>My code as of now is:</p>

<pre><code>def Button_1():
    p = subprocess.Popen(['play','playback.wav'])

def Button_2():
    p.send_signal(signal.SIGNIT)
</code></pre>

<p>Use <a href="https://docs.python.org/2/library/subprocess.html#subprocess.Popen.poll" rel="nofollow"><code>p.poll()</code></a> to check if the process is still running.</p>

<p>The code will look something like this (untested):</p>

<pre><code>def Button_1():
    if p.poll():
        p.send_signal(signal.SIGNIT)
    else:
        p = subprocess.Popen(['play','playback.wav'])

def Button_2():
    p.send_signal(signal.SIGNIT)
</code></pre>

python subprocess kill sox