在从属模式下运行 mplayer 时,我尝试通过管道向 mplayer 发送命令,如下所示:
import subprocess, time
# start mplayer
song = 'mysong.mp3'
cmd = ['mplayer', '-slave', '-quiet', song]
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stdin=subprocess.PIPE)
# send a command every 3 seconds.
# Full command reference here: http://www.mplayerhq.hu/DOCS/tech/slave.txt
while True:
print('sleep 3 seconds ...')
time.sleep(3)
cmd = 'get_meta_artist'
print('send command: {}'.format(cmd))
p.stdin.write(cmd)
output = p.communicate()[0]
print(output)
但输出什么也没有。
我举的例子来自这个问题.
在终端中运行相同的 mplayer 命令效果很好。我在这里缺少什么?
UPDATE:
我将命令从“get_meta_artist”更改为“get_meta_artist\n”,以便换行符也被发送到管道,但在输出中仍然没有任何内容。
UPDATE2:
我将cmd更改为“\npause\n”,音乐暂停了。这意味着通过标准输入发送命令是有效的。这意味着“\nget_meta_artist\n”命令的输出字符串没有按预期返回......
你只能使用.communicate()
每个子进程一次。所以在一个while
循环不起作用。
相反,您应该解析以下输出p.stdout
直接地。如果有答案的话,似乎每个答案只有一行。
为了防止阻塞,您有 3 个选择:
使用线程。你有一个单独的线程来读取p.stdout
并将其数据发送到主线程。如果没有可用数据,它会阻塞。
-
Set p.stdout
到非阻塞模式。本质上,你必须这样做:
import fcntl, os
fcntl.fcntl(p.stdout.fileno(), fcntl.F_SETFL,
fcntl.fcntl(p.stdout.fileno(), fcntl.F_GETFL) | os.O_NONBLOCK)
如果您在没有可用数据的情况下阅读,则会出现异常(IOError: [Errno 11] Resource temporarily unavailable
).
与 一起工作select.select()
: 履行p.stdout.readline()
除非select.select([p.stdout], [], [], <timeout>)[0]
是一个非空列表。在这种情况下,保证给定的文件对象有可用的数据并且在读取时不会阻塞。
为了将“垃圾输出”与“有用”输出分开,您可以这样做:
def perform_command(p, cmd, expect):
import select
p.stdin.write(cmd + '\n') # there's no need for a \n at the beginning
while select.select([p.stdout], [], [], 0.05)[0]: # give mplayer time to answer...
output = p.stdout.readline()
print("output: {}".format(output.rstrip()))
split_output = output.split(expect + '=', 1)
if len(split_output) == 2 and split_output[0] == '': # we have found it
value = split_output[1]
return value.rstrip()
然后做
print perform_command(p, 'get_meta_artist', 'ANS_META_ARTIST')
print perform_command(p, 'get_time_pos', 'ANS_TIME_POSITION')
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)