简体   繁体   English

Python子进程,在定时延迟后终止进程

[英]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. 我是第一次使用Python和Raspberry Pi(这是Pi 2),并试图触发一组定时命令。 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" -触发名为“倒数”的音频文件(.wav)

-- Trigger another audio file (.wav) called "dixie" -触发另一个名为“ dixie”的音频文件(.wav)

-- While dixie is playing trigger a wget command to trigger a photo on my camera -在dixie播放时触发wget命令以触发相机上的照片

-- Keep playing "dixie" until the previous wget finishes executing -继续播放“ dixie”,直到上一个wget完成执行

-- When wget finishes executing, stop playing "dixie" -wget完成执行后,停止播放“ dixie”

-- Trigger final audio file (.wav) called "applause" -触发名为“掌声”的最终音频文件(.wav)

-- 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. 本质上,wget是重要的,播放音频文件只是为了在我的相机拍摄照片时创造音乐。 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). 当wget完成并且掌声结束时,我想杀死所有音频,但是“ dixie”的subprocess.Popen命令继续播放(大约40秒长)。 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. 我要在“掌声”播放完毕后杀死“ dixie”。

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? 有什么建议吗?

I would suggest doing this: 我建议这样做:

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

# do something 

proc.terminate()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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