栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Python:在从属模式下将命令发送到mplayer

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python:在从属模式下将命令发送到mplayer

.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 temporarilyunavailable
)。

  1. 使用
    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')


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/596486.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号