Python:在slave模式下向mplayer发送命令

kak*_*kyo 2 python subprocess pipe 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)
Run Code Online (Sandbox Code Playgroud)

但输出没什么.

我从这个问题中拿了例子.

在终端中运行相同的mplayer命令工作正常.我在这里错过了什么?

更新:

我将cmd从"get_meta_artist"更改为"get_meta_artist \n",以便将换行符发送到管道,但输出中仍然没有任何内容.

UPDATE2:

我将cmd更改为"\npause \n",音乐暂停了.这意味着通过stdin发送命令工作.这意味着"\nget_meta_artist \n"命令的输出字符串未按预期传送回来....

glg*_*lgl 5

.communicate()每个子进程只能使用一次.因此在while循环中使用它不起作用.

相反,您应该p.stdout直接解析输出.如果有答案,每个答案似乎有一行.

为了防止阻塞,您有3个选项:

  1. 使用线程.您有一个单独的线程,p.stdout它从主线程读取并将其数据发送到主线程.如果没有可用数据,它会阻止.

  2. 设置p.stdout为非阻塞模式.基本上,你必须这样做:

    import fcntl, os
    fcntl.fcntl(p.stdout.fileno(), fcntl.F_SETFL,
        fcntl.fcntl(p.stdout.fileno(), fcntl.F_GETFL) | os.O_NONBLOCK)
    
    Run Code Online (Sandbox Code Playgroud)

    如果您在没有可用数据的情况下阅读,则会出现异常(IOError: [Errno 11] Resource temporarily unavailable).

  3. 使用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()
Run Code Online (Sandbox Code Playgroud)

然后呢

print perform_command(p, 'get_meta_artist', 'ANS_META_ARTIST')
print perform_command(p, 'get_time_pos', 'ANS_TIME_POSITION')
Run Code Online (Sandbox Code Playgroud)