amoeboar
amoeboar

Reputation: 355

Python subprocess, kill process after timed delay

I'm working with Python and Raspberry Pi for the first time (it's a Pi 2) and trying to trigger a timed set of commands. I've got most of it figured out except the very end, where I want to kill all processes.

The logic is as follows:

-- Trigger an audio file (.wav) called "countdown"

-- Trigger another audio file (.wav) called "dixie"

-- While dixie is playing trigger a wget command to trigger a photo on my camera

-- Keep playing "dixie" until the previous wget finishes executing

-- When wget finishes executing, stop playing "dixie"

-- Trigger final audio file (.wav) called "applause"

-- Stop all audio

Essentially, the wget is the important one, the audio files playing are just to create music while my camera takes a photo. When the wget has finished, and the applause finishes, I want to kill all the audio, but the subprocess.Popen command for "dixie" continues to play (it's about 40 seconds long). How can I kill this process at the end?

Here is my code so far:

import os
import time
import subprocess

subprocess.call(["aplay countdown.wav"], shell=True)
subprocess.Popen(["aplay dixie.wav"], shell=True)
subprocess.call(["wget 'http://10.5.5.9/camera/SH?t=12345678&p=%01' -O-"], shell=True)
time.sleep(5)
subprocess.call(["aplay applause.wav"], shell=True)
subprocess.Popen.kill(["aplay dixie.wav"], shell=True)

I want to kill "dixie" once "applause" has finished playing.

My code yields the error:

"unbound method kill() must be called with Popen instance as first
argument (got list instance instead)"

Any suggestions out there?

Upvotes: 0

Views: 1066

Answers (1)

ljk321
ljk321

Reputation: 16770

I would suggest doing this:

proc = subprocess.Popen(["aplay dixie.wav"], shell=True)

# do something 

proc.terminate()

Upvotes: 0

Related Questions