Jun 25, 2015

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

No comments:

Post a Comment