Python:在从属模式下向mplayer发送命令
在从属模式下运行mplayer时,我试图通过管道将命令发送到mplayer,如下所示:Python:在从属模式下向mplayer发送命令,python,subprocess,pipe,mplayer,Python,Subprocess,Pipe,Mplayer,在从属模式下运行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 sec
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命令可以正常工作。我错过了什么
更新:
我将cmd从“get_meta_Artister”更改为“get_meta_Artister\n”,以便也向管道发送换行符,但输出中仍然没有任何内容
更新2:
我将cmd改为“\n使用\n”,音乐暂停。这意味着通过stdin发送命令是有效的。这意味着“\nget\u meta\u artist\n”命令的输出字符串没有按预期通过管道传回…每个子进程只能使用
.communicate()
一次。因此,在循环中使用它是行不通的
相反,您应该直接解析p.stdout
的输出。如果有答案,每个答案似乎只有一行
为了防止阻塞,您有3个选项:
使用线程。您有一个单独的线程,它读取p.stdout
,并将其数据发送到主线程。如果没有可用的数据,它将阻塞
将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]资源暂时不可用
)
使用select.select()
:仅当select.select([p.stdout],[],[],)[0]
为非空列表时,才执行p.stdout.readline()
。在这种情况下,给定的文件对象保证有可用的数据,并且在读取时不会阻塞
为了将“垃圾输出”与“有用”输出分开,您可以这样做:
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')
我现在这样做,我开始得到输出:
while True:
cmd = '\nget_meta_artist\n'
p.stdin.write(cmd)
output = p.stdout.readline()
print("output: {}".format(output.rstrip()))
sys.stdout.flush()
尽管我仍然需要找出一种绕过MPELL自己初始化STDUT的第一次刷新的方法,但是我认为我的问题已经解决了。
感谢GLGL给了我有用的提示。谢谢,这解释了为什么我没有看到重复出现的行为。请您再解释一下“将std*对象的ne切换到非阻塞模式”好吗???@kakyo I添加了一些解释。非常感谢您的详细回答。我做得很好。我刚刚读了一些关于网络编程中的select和non-blockingsocket的资料,直到现在才知道它是如何扩展到本地进程的。我需要更多地了解这一点。