views:

384

answers:

2

I tried these two methods:

os.system("python test.py")

subprocess.Popen("python test.py", shell=True)

Both approaches need to wait until test.py finishes which blocks main process. I know "nohup" can do the job. Is there a Python way to launch test.py or any other shell scripts and leave it running in background?

Suppose test.py is like this:

for i in range(0, 1000000):
    print i

Both os.system() or subprocess.Popen() will block main program until 1000000 lines of output displayed. What I want is let test.py runs silently and display main program output only. Main program may quie while test.py is still running.

+3  A: 

subprocess.Popen(["python", "test.py"]) should work.

Note that the job might still die when your main script exits. In this case, try subprocess.Popen(["nohup", "python", "test.py"])

Aaron Digulla
subprocess.Popen(["python", "test.py"]) just launches test.py and blocks main process by waiting for the output of test.py.
jack
That should only happen if you request `stdout=PIPE` as well. Which version of Python? Can you add a `print "xxx"` after the call to Popen to check that it really blocks?
Aaron Digulla
I just tested the code above and it works for me with Python 2.5.2 on Windows. Your problem must be something else.
Aaron Digulla
i tried again and it worked. sorry about the mistake and thanks for your answer.
jack
+1  A: 
os.spawnlp(os.P_NOWAIT, "path_to_test.py", "test.py")
Viswanadh
it works, but is it possible to let test.py runs silently?
jack
silently? do you mean you don't want to see the output on screen?
Viswanadh
yes, i dont want to see the output on screen. can i be done by adding ">> /dev/null" to the code?
jack
Unfortunately I think os.spawn*() doesn't support output redirection!
Viswanadh
`os.spawn*` have been deprecated by the subprocess module.
Aaron Gallagher