Showing posts with label Spawn New Process. Show all posts
Showing posts with label Spawn New Process. Show all posts

Jun 25, 2015

Python Spawn New Process SubProcess don't wait

Spawn New Process:
You can spawn a new process within an another process
You can make use of subprocess to start a new process

proc = Popen([python process_doc.py], shell=True, stdin=None, stdout=None, stderr=None, close_fds=True)

close_fds makes the parent process file handles inaccessible for the child

Once it hits Popen, it startes daemon process and coninues execution (parallely daemon process will coninue executing)

Example:
.....
print "Before Daemon Process to Start"
proc = Popen([python process_doc.py], shell=True, stdin=None, stdout=None, stderr=None, close_fds=True)
print "After Daemon Process to Start"

.....

In the above example, the parent process don't wait for child process (process_doc.py) to wait till it executes completely. It spawns new daemon process and continues.
This daemon process executes separately, finish the task and dies.

Run command in Shell:
cmd = 'python /tmp/test.py'
subprocess.call(cmd, shell=True) #Synchronous
subprocess.Popen(cmd, shell=True, executable='/bin/bash') #Asynchronous ****