【问题标题】:Python: Send command to mplayer under slave modePython:从模式下向mplayer发送命令
【发布时间】:2013-03-29 05:25:10
【问题描述】:

当在从模式下运行时,我试图通过管道向 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)

但是输出什么都没有。

我的例子来自this question

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

更新:

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

更新 2:

我把 cmd 改成“\npause\n”,音乐就暂停了。所以这意味着通过标准输入发送命令有效。这意味着“\nget_meta_artist\n”命令的输出字符串没有按预期返回......

【问题讨论】:

    标签: python subprocess pipe mplayer


    【解决方案1】:

    每个子进程只能使用一次.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)
      

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

    3. 使用select.select():仅当select.select([p.stdout], [], [], <timeout>)[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')
    

    【讨论】:

    • 谢谢,这解释了为什么我没有看到重复出现的行为。你能解释一下“将 ne 的 std* 对象切换到非阻塞模式”吗??
    • @kakyo 我添加了一些解释。
    • 非常感谢您的详细回答。我工作完美。我刚刚阅读了一些关于网络编程中选择和非阻塞套接字的材料,直到现在才知道它如何扩展到本地进程的东西。我需要了解更多相关信息。
    【解决方案2】:

    我现在正在这样做,我开始得到输出:

     while True:
        cmd = '\nget_meta_artist\n'
        p.stdin.write(cmd)
        output = p.stdout.readline()
        print("output: {}".format(output.rstrip()))
        sys.stdout.flush()
    

    虽然我仍然需要想办法绕过 mplayer 自己的初始化标准输出的第一次刷新,但我认为我的问题已经解决了。

    感谢 glglgl 给我有用的提示。

    【讨论】:

      猜你喜欢
      • 2011-12-18
      • 1970-01-01
      • 2022-06-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-30
      相关资源
      最近更新 更多