Python: Send command to mplayer under slave mode

久未见 提交于 2019-12-02 01:13:12

You can only use .communicate() once per subprocess. So using it in a while loop doesn't work.

Instead, you should parse the output of p.stdout directly. There seems to be one line per answer if there is an answer.

In order to prevent blocking, you have 3 options:

  1. Work with threads. You have a separate thread who reads from p.stdout and sends its data to the main thread. It blocks if no data is available.

  2. Set p.stdout to non-blocking mode. Essentially, you'll have to do this:

    import fcntl, os
    fcntl.fcntl(p.stdout.fileno(), fcntl.F_SETFL,
        fcntl.fcntl(p.stdout.fileno(), fcntl.F_GETFL) | os.O_NONBLOCK)
    

    If you read then without data being available, you get an exception (IOError: [Errno 11] Resource temporarily unavailable).

  3. Work with select.select(): perform p.stdout.readline() only if select.select([p.stdout], [], [], <timeout>)[0] is a non-empty list. In that case, the given file object is guaranteed to have data available and to not block on reading.

In order to separate "garbage output" from "useful" output, you could do this:

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()

and then do

print perform_command(p, 'get_meta_artist', 'ANS_META_ARTIST')
print perform_command(p, 'get_time_pos', 'ANS_TIME_POSITION')

I'm now doing it this way and I start to get output:

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

Although I still need to figure out a way to bypass the first flush of mplayer's own initialization stdout but I consider my problem solved.

Thanks to glglgl for giving me useful hints.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!