Subprocess.Popen 在几秒钟后停止(或发生故障) [英] Subprocess.Popen stops ( or malfunctions) after a few seconds

查看:98
本文介绍了Subprocess.Popen 在几秒钟后停止(或发生故障)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个完整的初学者,所以对任何错误表示歉意.这是我在 Python 3.5 中的代码.它在 Raspberry Pi 3 上的 Raspbian 中执行.

I am a complete beginner so apologies for any mistakes. This is my code in Python 3.5. It executes in Raspbian on a Raspberry Pi 3.

import subprocess

radio = subprocess.Popen(["mplayer", 'http://edge-bauerabsolute-02-gos1.sharp-stream.com/absolute90s.mp3?'], shell = False , stdout=subprocess.PIPE)

print ("Other code - no waiting for the subprocess to finish")

收音机播放大约 30 秒然后停止.我希望它在没有脚本等待子进程结束的情况下在后台运行.另外,在 Linux 中,如果我停止脚本,收音机会作为 mplayer 的运行进程再次返回(所以 python 脚本必须以某种方式停止它?)

The radio plays for about 30 seconds and then stops. I want it to run in the background without the script waiting for the subprocess to end. Also, while in Linux, if I stop the script the radio comes back again as a running process of mplayer (so the python script must be stopping it somehow?)

似乎子进程仍在继续,但音乐/声音停止了.它似乎与互联网连接无关,即使我等待它也不会再次启动.我试过做 radio.communicate() 或 radio.stdout.read() 这很有趣,让我的收音机连续播放,但不继续脚本.我没有任何输出,脚本只是保留.

It seems as if the subprocess continues but the music/sound stops. It does not seem to be internet connection related, also if I wait it it does not start again. I have tried doing radio.communicate() or radio.stdout.read() which funny enough lets my radio play continuously, but doesn't continue the script. I have no output from either, the script just holds.

问题:当脚本执行其他操作时,我如何允许广播"进程在后台继续(同时播放音乐)?

Question: How do I allow the 'radio' process to continue in the background (with the music playing too) while the script does other things?

推荐答案

幸运的是我自己解决了.subprocess.PIPE 显然会停止/干扰进程,所以我用 DEVNULL 代替了 stdout=subprocess.PIPE:

I have solved it myself luckily. The subprocess.PIPE apparently stops/interferes with the process so instead of stdout=subprocess.PIPE I have done DEVNULL like this:

DEVNULL = open(os.devnull, 'wb')
radiostream = subprocess.Popen(["mplayer", 'http://edge-bauerabsolute-02-gos1.sharp-stream.com/absolute90s.mp3?&'], shell = False, stdout=DEVNULL, stderr=DEVNULL)

这篇关于Subprocess.Popen 在几秒钟后停止(或发生故障)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆