Tuesday, June 10, 2014

Parallel Processing in PHP - Threads

With the kind of compute resources we have in today's world processing which took hours and days have become a matter of hours. With AWS, Azure and other cloud providers its easy to ramp up and down on resources. Not only do I get my work done faster but also cheaper.

Lets see how its done on PHP.

PHP has multiple ways to allow things to be done parallelly each with its positives and negatives. Lets mention a few ways it can be done before we start...
pthreads
pcntl_fork
popen
fopen
curl
fsockopen

Lets try out pthreads...

Our Problem
Large List
Web API
DB response
= Long Running Time



Solution
Large List
Web API
DB response
Parallel Processing
= Quick work done.



So assuming we have 4 processors what would otherwise be
Loop {
  $i = Read Item
  Fire Web API (i)
  Write DB
}

VS the smarter way

processApi(i) {
    Fire Web API (i)
    Write DB
}
$pipe = 4
$process = 0
$count = 0
Loop
  If ($process < $pipe) {
    $i = Read Item
    $process++
    $threads[$count] = new Thread( 'processApi' )
    $threads[$index]->start( $i );
  } else {
    foreach ($threads as $key->$value){
      if( ! $value->isAlive() ) {
        unset( $threads[$key] );
        $process--;
      }
    }
    sleep (1);
  }

This is a very rough example without error checks etc but should give an example to any developer.