Python subprocess, kill process after timed delay

喜你入骨 提交于 2019-12-25 03:37:25

问题


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?


回答1:


I would suggest doing this:

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

# do something 

proc.terminate()


来源:https://stackoverflow.com/questions/30470759/python-subprocess-kill-process-after-timed-delay

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!