Here's a more substantial example of how to use the run functional API.
<?php
use \parallel\{Runtime, Future, Channel, Events};
$minId = 11001;
$maxId = 1382130;
$workers = 8;
$totalIds = $maxId - $minId;
$batchSize = ceil($totalIds / $workers);
$lastBatch = $totalIds % $batchSize;
$rowsToFetch = 5000;
print "Total IDs: " . $totalIds . "\n";
print "Batch Size: " . $batchSize . "\n";
print "Last Batch: " . $lastBatch . "\n";
$producer = function(int $worker, int $startId, int $endId, int $fetchSize) {
$tempMinId = $startId;
$tempMaxId = $tempMinId + $fetchSize;
$fetchCount = 1;
print "Worker " . $worker . " working on IDs from " . $startId . " to " . $endId . "\n";
while($tempMinId < $endId) {
for($i = $tempMinId; $i < $tempMaxId; $i++) {
$usleep = rand(500000, 1000000);
usleep($usleep);
print "Worker " . $worker . " finished batch " . $fetchCount . " from ID " . $tempMinId . " to " . $tempMaxId . "\n";
break;
}
$tempMinId = $tempMaxId;
$tempMaxId = $tempMinId + $fetchSize;
if($tempMaxId > $endId) {
$tempMaxId = $endId;
}
$sleep = rand(1,5);
sleep($sleep);
$fetchCount++;
}
print "Worker " . $worker . " finished\n";
};
for($i = 0; $i < $workers; $i++) {
$startId = $minId + ($i * $batchSize);
$endId = $startId + $batchSize;
if($i == ($workers - 1)) {
$endId = $maxId;
}
\parallel\run($producer, array(($i+1), $startId, $endId, $rowsToFetch));
}
?>