Rick
Rick

Reputation: 17013

Python, using os.system - Is there a way for Python script to move past this without waiting for call to finish?

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")

Upvotes: 6

Views: 3850

Answers (3)

Dominik Szopa
Dominik Szopa

Reputation: 1929

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.

Upvotes: 2

gspr
gspr

Reputation: 11227

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.

Upvotes: 9

tamasd
tamasd

Reputation: 5913

Try:

os.system("command_to_start_server &>/dev/null &")

Upvotes: 0

Related Questions