问题
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