Python、mpg123 和子进程未正确使用 stdin.write 或通信 [英] Python, mpg123 and subprocess not properly using stdin.write or communicate

查看:81
本文介绍了Python、mpg123 和子进程未正确使用 stdin.write 或通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我的 google-fu 真的很糟糕,我找不到答案,希望你们能帮助我 ^_^

ok, so my google-fu really kind of sucks and I was unable to find an answer, hopefully you folks can help me ^_^

好的,所以我认为一个简单的脚本似乎没有正确地与其子进程通信,我正在一行一行地运行这个.我也在用 mpg123 播放器,这是一个 Linux 系统(嗯,树莓派)

ok, so what I thought would be a simple script is seemingly not communicating with its subprocess correctly, I'm running this line by line. I'm also using the mpg123 player, this is a Linux system (well, Raspberry Pi)

    from subprocess import Popen, PIPE, STDOUT
    p = Popen(["mpg123", "-C", "test.mp3"], stdout=PIPE, stdin=PIPE, stderr=STDOUT)
    #wait a few seconds to enter this, "q" without a newline is how the controls for the player work to quit out if it were ran like "mpg123 -C test.mp3" on the command line
    p.communicate(input='q')[0]

我可以在它上面运行 stdout.read() 就好了,但是使用通信进行输入只会让它挂起,而 p.stdin.write('q') 似乎什么也没做.这是与 python 相关的,尽管我有一种感觉,我也没有在 mpg123 文档中找到正确的位置.请善待,因为我对此非常陌生^_^

I can run stdout.read() on it just fine, but using communicate for input just makes it hang and p.stdin.write('q') does seemingly nothing at all. This is python related though I have a feeling I'm also not looking in the right place in the mpg123 documentation. Please be kind as I'm exceptionally new to this ^_^

推荐答案

这是一个糟糕的解决方案,但它在紧要关头起作用.我将它用作补丁是因为出于某种原因,我无法让 Python 库在我的 Raspberry Pi 上正常运行.

This is an awful solution, but it works in a pinch. I'm using this as a patch because for some reason, I cannot get Python libraries to play properly on my Raspberry Pi.

如果您在远程模式下启动 mpg123 (mpg123 -R),您可以更轻松地向其发送命令:

If you start mpg123 in remote mode (mpg123 -R), you can send commands to it far more easily:

proc = sp.Popen(["mpg123", "-R"], stdin=sp.PIPE)

然后,您可以向其 stdin 属性发送命令.

Then, you can send commands to its stdin attribute.

注意:

  • 命令不同.暂停是"pause",而不是"" 例如.在控制台中运行 mpg123 -R,然后向其发送 help 命令以查看命令列表.
  • 命令需要以换行符结束.
  • The commands are different. To pause, it's "pause", not " " for example. Run mpg123 -R in a console, then send it the help command to see a list of commands.
  • The commands need to be newline terminated.

来自文档:

-R, --remote

-R, --remote

激活通用控制界面.mpg123 然后将从标准输入读取和执行命令.基本用法是 ''load'' 播放一些文件和明显的 ''pause'', ''command.''jump'' 将跳转/寻找给定点(MPEG 帧号).发出帮助"以获取完整的命令和语法列表.

Activate generic control interface. mpg123 will then read and execute commands from stdin. Basic usage is ''load '' to play some file and the obvious ''pause'', ''command. ''jump '' will jump/seek to a given point (MPEG frame number). Issue ''help'' to get a full list of commands and syntax.

这篇关于Python、mpg123 和子进程未正确使用 stdin.write 或通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆