Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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")
like image 653
Rick Avatar asked Sep 30 '10 11:09

Rick


People also ask

Does OS system wait for command to finish?

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.

How do I run a Python command in OS?

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.

What does OS system do in Python?

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.

Does OS system return anything Python?

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() .


Video Answer


2 Answers

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.

like image 197
gspr Avatar answered Oct 12 '22 23:10

gspr


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.

like image 27
Dominik Szopa Avatar answered Oct 12 '22 23:10

Dominik Szopa