如何在后台运行Python子进程管道然后终止它

时间:2015-08-26 08:25:08

标签: python pipe subprocess kill communicate

我希望语音合成程序节能够生成声音,直到它被杀死。在Bash中,我正在尝试做的事情如下:

>cat /var/log/dmesg | festival --tts &
[1] 27262
>kill -9 27262

当我尝试使用subprocess执行此操作时,我的代码在使用通信功能时挂起。我该如何解决这个问题?应该立即杀死这个过程。

raw_input(
    "Ready to set Festival running...\n" +\
    "Press Enter to continue."
)
process_cat = subprocess.Popen([
    "cat",
    "/var/log/dmesg"
], stdout = subprocess.PIPE)
process_Festival = subprocess.Popen([
    "festival",
    "--tts"
], stdin = process_cat.stdout, stdout = subprocess.PIPE)
process_Festival.communicate()[0]
raw_input(
    "Ready to stop Festival running...\n" +\
    "Press Enter to continue."
)
os.kill(process_cat.pid, signal.SIGKILL)
os.kill(process_Festival.pid, signal.SIGKILL)

0 个答案:

没有答案