【问题标题】:Send a character to subprocess.Popen向 subprocess.Popen 发送一个字符
【发布时间】:2016-11-04 17:36:35
【问题描述】:

我正在尝试使用 python 脚本在我的 rpi3 上的命令行上控制 omxplayer,我调用 subprocess.Popen 并对其进行控制(例如播放/暂停、增加/减少音量等)。我应该写一个字符到stdin(例如p,+/-等),该字符作为变量文本在send_signal中传递,使用通信我无法控制它,有没有更好的方法?

编辑:还有 player.stdin.write(text); player.stdin.flush() 不起作用。

def start_music():
    player = subprocess.Popen(['omxplayer', songs[0]], stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.STDOUT)
    return player

def send_signal(player, text):
    player.communicate(text.encode('utf-8'))
    player.stdin.close()

尝试使用 cat -e 而不是 omxplayer 输出是:

why_dont_you work
test_input
test_input$
test_input2
test_input2$
a
a$

请注意,第一行不再显示

【问题讨论】:

  • omxplayer 是 GUI 还是命令行程序?
  • @NickT 在命令行
  • 你试过player.stdin.write(text); player.stdin.flush()吗?
  • @HaiVu 是的,这也行不通
  • “不起作用”到底是什么意思?其他进程不会将该字符写入标准输入吗?

标签: python subprocess communicate omxplayer


【解决方案1】:

需要指定您传递的是输入变量,即

def send_signal(player, text):
    player.communicate(input=text.encode('utf-8'))
    player.stdin.close()

【讨论】:

    猜你喜欢
    • 2016-11-06
    • 2012-12-24
    • 2021-06-22
    • 2021-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多