Coder Perfect

Using python to run command-line scripts [duplicate]


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.


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. But is there other ways to make these things more elegant?

Asked by Mattias

Solution #1

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']))

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 pull requests from the database and use sox, then send the resulting output file to where it needs to be.

Answered by Dale Reidy

Solution #4

I’m not familiar with Sox, but is it possible to put it up as a service and connect to it for requests instead of repeatedly calling the application as a command line? For example, sqlite is a connection interface that you may look at for ideas.

Answered by z –

Post is based on