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")
Answer #1: os. system() does wait for its process to complete before returning. If you are seeing it not wait, the process you are launching is likely detaching itself to run in the background in which case the subprocess.
The naive approach to run a shell command is by using os. system(): Let's first create a new Python file called shell_cmd.py or any name of your choice. Second, in the Python file, import the os module, which contains the system function that executes shell commands.
The OS module in Python provides functions for creating and removing a directory (folder), fetching its contents, changing and identifying the current directory, etc. You first need to import the os module to interact with the underlying operating system.
os. system() returns the (encoded) process exit value. 0 means success: On Unix, the return value is the exit status of the process encoded in the format specified for wait() .
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With