Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If you really need the controls, you can't just use <code>Popen</code>.</p> <p><code>mpg123</code> only enables terminal control if its stdin is a tty, not if it's a file or pipe. That's why you get this line in the banner:</p> <pre><code>Terminal control enabled, press 'h' for listing of keys and functions. </code></pre> <p>And the whole point of <code>Popen</code> (and <code>subprocess</code>, and the POSIX APIs it's built on) is pipes.</p> <p>So, what can you do about it?</p> <hr> <p>On linux, you can use the <a href="http://docs.python.org/3/library/pty.html" rel="nofollow"><code>pty</code></a> module. It may also work on other *nix platforms, but it may not—even if it gets built and included in your stdlib. As the docs say:</p> <blockquote> <p>Because pseudo-terminal handling is highly platform dependent, there is code to do it only for Linux. (The Linux code is supposed to work on other platforms, but hasn’t been tested yet.)</p> </blockquote> <p>It definitely runs on *BSD platforms on 2.7 and 3.3, and the example in the docs seem to work on both Mac OS X and FreeBSD… but that's as far as I've checked.</p> <hr> <p>Meanwhile, most POSIX platforms will at least have <a href="http://docs.python.org/3.3/library/os.html#os.forkpty" rel="nofollow"><code>os.forkpty</code></a>, and that's not much harder, so here's a trivial program that plays the first 5 seconds of a song passed as its first arg:</p> <pre><code>import os import pty import sys import time pid, fd = os.forkpty() if pid: time.sleep(5) os.write(fd, 'q') else: os.spawnl(os.P_WAIT, # mode '/usr/local/bin/mpg123', # path '/usr/local/bin/mpg123', '-C', sys.argv[1]) # args </code></pre> <hr> <p>Note that I used <code>os.spawnl</code> above. This is probably <em>not</em> what you want in a real program; it's for pedagogic purposes, to encourage you to read the docs (and the corresponding manpages) and understand this family of functions.</p> <p>As <a href="http://docs.python.org/3/library/os.html#os.spawnl" rel="nofollow">the docs</a> explain, this does not use the <code>PATH</code> environment variable, so you need to specify the full path to the program. You can just use <code>spawnlp</code> instead of <code>spawnl</code> to fix this.</p> <p>Also, <code>spawn</code> may (in fact, always does, although the docs aren't entirely clear) do another fork to execute the child. This really isn't necessary, but <code>spawn</code> does things that you would need to do manually if you just called <code>exec</code>. If you know what you're doing, you may well want to use <code>execl</code> (or <code>execlp</code>) instead of <code>spawnl</code>.</p> <p>You can even use most of the functionality in <code>subprocess</code> as long as you're careful (do not create any pipes, and remember that you'll end up doing <em>two</em> <code>fork</code>s, so make sure to set up the parent/child relationship properly).</p> <p>Also notice that you need to pass the path to <code>mpg123</code> <em>twice</em>—once as the path, and then once as the child program's <code>argv[0]</code>. You could also just pass <code>mpg123</code> the second time. Or, ideally, look at what <code>ps</code> says when you run it from the shell, and pass that. At any rate, you have to pass <em>something</em> as the <code>argv[0]</code>; otherwise, <code>-C</code> ends up being the <code>argv[0]</code>, which means mpg123 won't think you gave it a <code>-C</code> flag to enable control keys, but rather than you renamed it to <code>-C</code> and ran it with no flags…</p> <p>Anyway, you really do need to read the docs to understand what each of these functions does, instead of just treating it like magic code that you don't understand. So, I intentionally used the simplest possible solution to encourage that.</p> <hr> <p>On Windows, there is no such thing as a <code>pty</code>, and no way to do this at all with the facilities built in to Python. You will need to use one of the various third-party libraries for controlling a cmd.exe console (aka DOS prompt) instead.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload