gpt4 book ai didi

Python 子进程 Popen.terminate() 仍然停留在 wait()

转载 作者:太空宇宙 更新时间:2023-11-03 11:23:35 24 4
gpt4 key购买 nike

我正在尝试使用 RPi 实现广播播放器。目标是设置播放列表并在填充播放列表后开始播放。一旦执行停止代码,播放器和 radio 进程将退出。

radio 进程正常终止,但播放器进程即使在调用终止后仍处于等待状态。如果再次调用停止代码,则播放器进程终止

尝试过的事情:

  1. 重新排序等待命令(播放器、 radio )/( radio 、播放器)
  2. 类似地重新排序终止命令
  3. 使用 kill 而不是 terminate 挂起 RPi

玩家代码:

while playlist:
player = subprocess.Popen(
["avconv", "-i", "Music/%s" % playlist[0], "-f", "s16le", "-ar",
"22.05k", "-ac", "1", "-"], stdout=subprocess.PIPE)

radio = subprocess.Popen(["./pifm", "-", freq], stdin=player.stdout)

radio.wait()
print "************ exiting from radio :)"
player.wait()
print "************ exiting from player :)"

playlist.pop(0)
player = None
radio = None

播放器停止代码(从另一个线程调用):

print "************ stop requested"

if radio and radio.poll() is None:
print "************ terminating radio :)"
radio.terminate()

if player and player.poll() is None:
print "************ terminating player :)"
player.terminate()

替代方案:

另一种选择是为广播和播放器提供一个持久的接收器

def start_radio():
global radio
radio = subprocess.Popen(["./pifm"...], stdin=subprocess.PIPE)

def play():
global player
while playlist and radio:
player = subprocess.Popen(["avconv"...], stdout=radio.stdin)
player.wait()
playlist.pop(0)

def stop():
if player and player.poll() is None:
print "************ terminating player :)"
player.terminate()

但在这种情况下,调用 player.terminate() 会关闭播放器,同时在 radio 过程中重复播放最后一个数据包(就像卡住的记录)。这张卡住的唱片一直播放到我开始播放新播放器或终止 radio 为止。

最佳答案

正如@J.F.Sebastian 提到的,使用player.stdout.close() 是可行的。整个代码库发布在这里 https://github.com/hex007/pifm_server

所以最终的代码看起来是这样的

while playlist:
player = subprocess.Popen(
["avconv", "-i", "Music/%s" % playlist[0], "-f", "s16le", "-ar",
"22.05k", "-ac", "1", "-"], stdout=subprocess.PIPE)

radio = subprocess.Popen(["./pifm", "-", freq], stdin=player.stdout)

player.stdout.close()

radio.wait()
player.wait()

if stop_requested:
stop_requested = False
break

playlist.pop(0)

player = None
radio = None

和停止代码:

stop_requested = True

if radio and radio.poll() is None:
radio.terminate()

if player and player.poll() is None:
player.terminate()

关于Python 子进程 Popen.terminate() 仍然停留在 wait(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38153635/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com