Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Subprocess.Popen在几秒钟后停止(或出现故障)_Python_Raspberry Pi_Subprocess_Raspbian_Mplayer - Fatal编程技术网

Python Subprocess.Popen在几秒钟后停止(或出现故障)

Python Subprocess.Popen在几秒钟后停止(或出现故障),python,raspberry-pi,subprocess,raspbian,mplayer,Python,Raspberry Pi,Subprocess,Raspbian,Mplayer,我是一个完全的初学者,所以为任何错误道歉。这是我在Python 3.5中的代码。它在树莓皮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 wai

我是一个完全的初学者,所以为任何错误道歉。这是我在Python 3.5中的代码。它在树莓皮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脚本必须以某种方式停止它?)

似乎子进程继续,但音乐/声音停止。它似乎与互联网连接无关,如果我等待它,它也不会再次启动。我试过做radio.communicate()或radio.stdout.read(),这很有趣,可以让我的收音机连续播放,但不会继续剧本。我没有从任何一个输出,脚本只是持有


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

我自己幸运地解决了这个问题。subprocess.PIPE显然会停止/干扰流程,因此我没有使用stdout=subprocess.PIPE,而是这样做:

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)

传递的
stdout=PIPE
允许您接收进程的输出。它进入一个缓冲区,你可以从中读取。这是一个有限的大小,因此如果不相对及时地从缓冲区读取,那么缓冲区将被填满,所有未来的输出将导致进程挂起,直到从缓冲区读取为止(这将释放空间)。如果只想隐藏输出,则可以传递更便于移植的
subprocess.DEVNULL
;然后子进程将与父进程(即Python程序)共享stdout/stderr。谁知道呢,也许有一天它会打印出一条有用的错误信息,丢弃它会是一种耻辱。Dunes,tripleee非常感谢你们两位。Dunes在查看子流程模块init.py中的缓冲区大小后,我完全按照您刚才所说的假设进行了假设。