Home > Code Snippets > Threading PHP scripts using OS facilities

Threading PHP scripts using OS facilities

September 30th, 2009 Leave a comment Go to comments

Recently while developing systems which consumes webservices, after a lot of optimizations were put into our code, we resorted to do some sort of benchmarking. From the begenning itself, we had proper loggers which were logging the activities to database tables, and verbose text files. For benchmarking of the systems we use xdebug, and our Open PHP Myprofiler. The code coverage analysis by xdebug, as well as cache grind showed our bottleneck was our webservice provider. While checking out, we also found that we could reduce the waiting time for webservices by requesting smaller data set from the webservice.

Okay, this was getting me somewhere. I thought why not write a cli snippet which would be triggered mutliple times with the exec function, and a dynamic configuration file passed as a parameter. Since we had all the webservice consumption code written with a lot of global variables and constants dependencies, we had a tough task to clean the code. Which we promptly set aside, and worked around by creating a configuration file dynamically using $var = unserialize(‘{serialize($var)}’), and writing the whole set of variables into a configuration file, and passing the file path to the instance. The exec was called with extra parameters ‘ > /dev/null & ‘, at the end.

The simple small snippet used the same database connection, and once data was fetched from the webservice, and processed, this was written into the database with a preagreed key, which would ofcourse be derived in the parent script, and passed to all the children through the dynamic configuration file. The parent script was sleeping in a loop and checking the database for any results which might have arrived. As soon as the most sensitive child finishes, and writes data to the cache in db, the parent script will fetch the data to the frontend. Thereby a periodic ajax call would update data from the other child processes which would complete in their own time.

The above system has helped us drastically to reduce response time in our Reserway Technologies projects and form the foundation for a handful of travel portals all over the world. One could taste our system through the demo site

Categories: Code Snippets Tags: ,
  1. No comments yet.
  1. No trackbacks yet.

− two = six