Thursday, December 20, 2012

PHP Script Execution Nature, A Problem and Solution

Do you know that a php script cannot be executed simultaneously on a single server. Even another script which is included with a executing script also becomes unavailable for execution or being included with other script. Usually a php script with complex algorithms and multiple mysql operations takes approximately 0.005 seconds for completing execution. For usual scripting with some mysql operations and an limited amount of users, the execution nature of apache server (PHP) is perfect. But, suppose, if a script has single or multiple API request to remote servers, then it will take some extra seconds to accept the reply. By the mean time other requests will be queued and will be processed while the script becomes free.

Recently, I have created a script which has more than 25 API requests and requires around 1-2 minutes for completing the whole execution. As the script has some dependencies over some universal scripts of my application, so I needed to change something to make the system always accessible for other users. First thing came to mind was to divide the whole execution and run it on multiple execution to make sure it frees the related scripts for other users for some while. I have used a Session Variable ($_SESSION) where I have put all information required for requesting API request. For every execution of the script, now it sends only a single API request, and then frees the script and redirect to the same script for rest of the requests.

No comments:

Post a Comment