The Python Oracle

Executing command line programs from within python

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: Lost Civilization

--

Chapters
00:00 Question
01:09 Accepted answer (Score 320)
01:31 Answer 2 (Score 28)
02:09 Answer 3 (Score 3)
02:39 Answer 4 (Score 2)
03:04 Thank you

--

Full question
https://stackoverflow.com/questions/4502...

Question links:
[sox]: http://sox.sourceforge.net/

Accepted answer links:
[subprocess]: http://docs.python.org/library/subproces...

Answer 2 links:
[ffmpegx]: http://homepage.mac.com/major4/
[GUI]: https://en.wikipedia.org/wiki/Graphical_...
[GUI]: https://en.wikipedia.org/wiki/Graphical_...
[GUI]: https://en.wikipedia.org/wiki/Graphical_...

Answer 4 links:
[sqlite]: http://docs.python.org/library/sqlite3.h...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #commandline

#avk47



ACCEPTED ANSWER

Score 323


The subprocess module is the preferred way of running other programs from Python -- much more flexible and nicer to use than os.system.

import subprocess
#subprocess.check_output(['ls', '-l'])  # All that is technically needed...
print(subprocess.check_output(['ls', '-l']))



ANSWER 2

Score 28


This whole setup seems a little unstable to me.

Talk to the ffmpegx folks about having a GUI front-end over a command-line backend. It doesn't seem to bother them.

Indeed, I submit that a GUI (or web) front-end over a command-line backend is actually more stable, since you have a very, very clean interface between GUI and command. The command can evolve at a different pace from the web, as long as the command-line options are compatible, you have no possibility of breakage.




ANSWER 3

Score 3


If you're concerned about server performance then look at capping the number of running sox processes. If the cap has been hit you can always cache the request and inform the user when it's finished in whichever way suits your application.

Alternatively, have the n worker scripts on other machines that pull requests from the db and call sox, and then push the resulting output file to where it needs to be.




ANSWER 4

Score 2


I am not familiar with sox, but instead of making repeated calls to the program as a command line, is it possible to set it up as a service and connect to it for requests? You can take a look at the connection interface such as sqlite for inspiration.