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 TimeWeb API
DB response
Solution
Large List
Web API
DB response
Parallel Processing
= Quick work done.Web API
DB response
Parallel Processing
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.