views:

89

answers:

3

I am trying to use Python (through Django framework) to make a Linux command line call and have tried both os.system and os.open but for both of these it seems that the Python script hangs after making the command line call as the call is for instantiating a server (so it never "finishes" as its meant to be long-running). I know for doing something like this with other Python code you can use something like celery but I figured there would be a simple way to get it to just make a command line call and not be "tied into it" so that it can just move past, I am wondering if I am doing something wrong... thanks for any advice.

I am making the call like this currently

os.system("command_to_start_server")

also tried:

response = os.popen("command_to_start_server")
A: 

Try:

os.system("command_to_start_server &>/dev/null &")
Yorirou
Thanks, that got it to work although its still giving some strange behavior as the output page in Django (even though its showing final output) keeps showing "Loading" indefinitely... but doing what you said got it to move past the part where it was "hanging"
Rick
now it redirects the entire output to /dev/null, so nothing will shown on the page
Yorirou
+7  A: 

I'm not sure, but I think the subprocess module with its Popen is much more flexible than os.popen. If I recall correctly it includes asynchronous process spawning, which I think is what you're looking for.

Edit: It's been a while since I used the subprocess module, but if I'm not mistaken, subprocess.Popen returns immediately, and only when you try to communicate with the process (such as reading its output) using subprocess.communicate does your program block waiting for output if there is none.

gspr
thanks, this sounds like a better solution than what I was doing... there is some strange "binding" type of behavior going on when I make cmd line calls the way I was through an object that is initiated originally from Django, the call I was making on cmd line was to start another test server (on diff port) but somehow that gets bound to the Django test server where it causes Django test server to fail even though I can normally run them both at the same time when i start them manually from cmd line
Rick
+2  A: 

You can use django-celery. django-celery provides Celery integration for Django. Celery is a task queue/job queue based on distributed message passing.

See this http://ask.github.com/celery/getting-started/first-steps-with-django.html for tutorial how to use it.

Dominik Szopa