Problem
I’m working on a web application that will edit sound files (pad, mix, combine, etc.) and I’ve discovered that sox performs exactly what I need. Sox is a linux command line software, and I’m not sure I want the python web app to launch additional sox processes on my server based on the number of requests.
Example:
import os
os.system('sox input.wav -b 24 output.aiff rate -v -L -b 90 48k')
This entire system appears to be a little shaky to me.
So, what is the best practice for running command line scripts from within a web app written in Python (or any other scripting language)?
To get around the entire request response cycle, message queues would be one thing to implement. Is there, however, any alternative method to make these items more elegant?
Asked by Mattias
Solution #1
os.system.
import subprocess
#subprocess.check_output(['ls', '-l']) # All that is technically needed...
print(subprocess.check_output(['ls', '-l']))
Answered by dF.
Solution #2
Speak with the ffmpegx team about a GUI front-end instead of a command-line backend. They don’t appear bothered by it.
Indeed, I contend that a GUI (or web) front-end is more stable than a command-line backend, because the interface between GUI and command is quite clean. You have no risk of breaking the command if it evolves at a different rate than the web, as long as the command-line arguments are consistent.
Answered by S.Lott
Solution #3
If server speed is a concern, consider limiting the number of running Sox processes. If the limit has been reached, you can always cache the request and notify the user when it’s completed in whatever way works best for your app.
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.
Answered by Dale Reidy
Solution #4
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? For example, sqlite is a connection interface that you may look at for ideas.
Answered by z –
Post is based on https://stackoverflow.com/questions/450285/executing-command-line-programs-from-within-python