Subprocess.Popen stops ( or malfunctions) after a few seconds

China☆狼群 提交于 2021-01-27 18:51:14

问题


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")

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?)

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?


回答1:


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)


来源:https://stackoverflow.com/questions/52297000/subprocess-popen-stops-or-malfunctions-after-a-few-seconds

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