Python 子进程 Popen.terminate() 仍然停留在 wait()
Python subprocess Popen.terminate() still stuck on wait()
我正在尝试使用 RPi 实现广播播放器。目标是设置播放列表并在填充播放列表后开始播放。执行停止代码后,播放器和无线电进程应退出。
无线电进程正常终止,但播放器进程即使在调用终止后仍处于等待状态。如果再次调用停止代码,则播放器进程终止
尝试过的事情:
- 重新排序等待命令(播放器、收音机)/(收音机、播放器)
- 类似地重新排序终止命令
- 使用 kill 而不是终止会挂起 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() 会关闭播放器,同时在无线电过程中重复播放最后一个数据包(就像卡住的记录)。这张卡住的唱片一直播放到我开始播放新播放器或终止收音机为止。
如@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()
我正在尝试使用 RPi 实现广播播放器。目标是设置播放列表并在填充播放列表后开始播放。执行停止代码后,播放器和无线电进程应退出。
无线电进程正常终止,但播放器进程即使在调用终止后仍处于等待状态。如果再次调用停止代码,则播放器进程终止
尝试过的事情:
- 重新排序等待命令(播放器、收音机)/(收音机、播放器)
- 类似地重新排序终止命令
- 使用 kill 而不是终止会挂起 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() 会关闭播放器,同时在无线电过程中重复播放最后一个数据包(就像卡住的记录)。这张卡住的唱片一直播放到我开始播放新播放器或终止收音机为止。
如@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()