338

I'm building a web application that will is going to manipulate (pad, mix, merge etc) sound files and I've found thatsox does exactly what I want. Sox is a linux command line program and I'm feeling a little uncomfortable with having the python web app starting new sox processes on my server on a per request basis.

Example:

import osos.system('sox input.wav -b 24 output.aiff rate -v -L -b 90 48k')

This whole setup seems a little unstable to me.

So my question is, what's the best practice for running command line programs from within a python (or any scripting language) web app?

Message queues would be one thing to implement in order to get around the whole request response cycle. But is there other ways to make these things more elegant?

askedJan 16, 2009 at 12:30
Mattias's user avatar
6
  • 7
    Related:stackoverflow.com/questions/89228/…,stackoverflow.com/questions/311601/…CommentedJan 16, 2009 at 12:55
  • > I'm feeling a little uncomfortable > with having the python web app > starting new sox processes on my > server on a per request basis. To me this seems to mean that he fears that, if he opens up his webserver to the public, that there's not a lot he can do to prevent his server resources from being consumed if 15.000 people decide to click on that button that will launch sox in this way.CommentedJan 16, 2009 at 14:39
  • 1
    @Thomas. I can't see how "web" changes anything in this case. Can you explain why "web" matters when running a subprocess?CommentedMay 11, 2011 at 17:00
  • 2
    @S.Lott The OP expresses the concern of starting a subprocess on a per-request basis. So it's not only about starting subprocesses, but also e.g. about limiting their overall number.CommentedMay 11, 2011 at 17:20
  • 1
    @ThomasH: "limiting their overall number"? It seemed more like the request-response of a web site is fast, but this subprocess might be slow. It's hard to tell. I don't get how the other references are not related.CommentedMay 11, 2011 at 17:35

4 Answers4

323

Thesubprocess module is the preferred way of running other programs from Python -- much more flexible and nicer to use thanos.system.

import subprocess#subprocess.check_output(['ls', '-l'])  # All that is technically needed...print(subprocess.check_output(['ls', '-l']))
Anonsage's user avatar
Anonsage
8,3406 gold badges54 silver badges54 bronze badges
answeredJan 16, 2009 at 12:48
dF.'s user avatar
Sign up to request clarification or add additional context in comments.

4 Comments

import subprocess; subprocess.check_output(['ls', '-l']). for using ls -l in the command line. check_output() return the output of the command too.
by usingsubprocess.check_call(['ls','-l']) you don't need to print the output.
what is wrong with using theos?
what if I want to pipe things e.g.pip list | grep anatome?
28

This whole setup seems a little unstable to me.

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

Indeed, I submit that aGUI (or web) front-end over a command-line backend is actually more stable, since you have a very, very clean interface betweenGUI 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.

gorandp's user avatar
gorandp
5936 silver badges12 bronze badges
answeredJan 16, 2009 at 12:58
S.Lott's user avatar

Comments

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.

answeredJan 16, 2009 at 18:11
Dale Reidy's user avatar

Comments

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 assqlite for inspiration.

answeredJan 16, 2009 at 12:36
z  -'s user avatar

1 Comment

You are spot on, but unfortunately sox does not run as a server daemon. At least not as far as I can tell.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.