Prev: Trying to redirect every urel request to test.py script with the visitors page request as url parameter.
Next: Compile python executable only for package deployment on Linux
From: loial on 20 Jul 2010 11:33 I have a requirement to kick off a shell script from a python script without waiting for it to complete. I am not bothered about any return code from the script. What is the easiest way to do this. I have looked at popen but cannot see how to do it.
From: Chris Rebert on 20 Jul 2010 13:32 On Tue, Jul 20, 2010 at 8:33 AM, loial <jldunn2000(a)gmail.com> wrote: > I have a requirement to kick off a shell script from a python script > without waiting for it to complete. I am not bothered about any return > code from the script. > > What is the easiest way to do this. I have looked at popen but cannot > see how to do it. Use the `subprocess` module. import subprocess proc = subprocess.Popen(["shell_script.sh", "arg1", "arg2"], stdout=subprocess.PIPE, stderr=subprcoess.PIPE) # lots of code here doing other stuff proc.wait() I believe you need to /eventually/ call .wait() as shown to avoid the child becoming a zombie process. Cheers, Chris -- http://blog.rebertia.com
From: Nobody on 20 Jul 2010 17:44
On Tue, 20 Jul 2010 10:32:12 -0700, Chris Rebert wrote: > I believe you need to /eventually/ call .wait() as shown to avoid the > child becoming a zombie process. Alternatively, you can call .poll() periodically. This is similar to ..wait() insofar as it will "reap" the process if it has terminated, but unlike .wait(), .poll() won't block if the process is still running. On Unix, you can use os.fork(), have the child execute the command in the background, and have the parent wait for the child with os.wait(). The child will terminate as soon as it has spawned the grandchild, and the grandchild will be reaped automatically upon termination (so you can forget about it). |