Showing posts with label subprocess. Show all posts
Showing posts with label subprocess. 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 ****

Python Asynchronous


Real Time Scenario:

In general, we process data synchrnously by default
To improve performance, we can separate data processing separately (if no dependency) by spawing a new process and use multi-threading or multi-tasking

E.g.,
Suppose, you have a webpage, on submit you have to upload many images/documents to remote location (Synchronously one by one it takes more time). When hundreds of requests keep coming, its difficult for the server to handle 

How can we achieve better results in Asynchronous way:
Once you select images/documents to upload and submit
Spawn a new process to start the processing of uploading images (this processing can be multi-threaded, multi-processing)
Keep updating the progress of the status to Database (1 of 5 completed, 2 of 5 completed etc..)
Browser keep checking for the status of the Database
Once the status is SUCCESS, initimate the user with Success message

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)

JQuery/JavaScript to keep checking Server for status:
function runner() {
     setTimeout(function() {
        $.ajax({
             url : "AJAX_POST_URL",
             type: "POST",
            data : formData,
            success: function(data, textStatus) {
                    if (data.status != 'Success') {
                        runner()
                    } else if  (data.status == 'Success') {
                        alert("Successfully Uploaded") 
                    }      
            },
        });
    }, time);
 }

runner();
 

Feb 20, 2014

python check process running

We make use of python subprocess module
  • To write/execute the command in the command line
  • To write to stdin and read the output of stdout

Methods Used
1) checkProcessRunning
        To check whether process in running or not

2) writeToCMD
        To execute the command in the command line and read the output of it

Pass the required arguments
checkProcessRunning(cmd = "adb shell ps", processName = "com.android.phone")

cmd -> Command to execute
processName -> Text to search from the command line output

How to Run
python python_check_process_running.py
import subprocess

def checkProcessRunning(cmd, processName):
    print "\n Process to check : " + cmd
    result = writeToCMD(cmd)
    if (result[0].rstrip().find(processName) != -1):
        print processName + " is present/running"
        return True
    else:
        print processName + " is not present/running"
        return False
          
def writeToCMD(cmd):
    proc = None       
    proc = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell="True")
    stdout_value = proc.communicate()
    if stdout_value:
        return stdout_value

checkProcessRunning(cmd = "adb shell ps", processName = "com.android.phone")