PHP code example of axxapy / php_threadpool
1. Go to this page and download the library: Download axxapy/php_threadpool library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
axxapy / php_threadpool example snippets
$Threads = [];
$Threads[] = new Thread(function(Thread $Thread) {
$Thread->saveData(file_get_contents($Thread->getLaunchParams()[0]));
});
$Threads[] = clone $Threads[0];
$Threads[] = clone $Threads[0];
$Threads[0]->run('http://ya.ru');
$Threads[1]->run('http://mail.ru');
$Threads[2]->run('http://r0.ru');
while (array_filter($Threads, function (Thread $Thread) {return $Thread->isChildAlive();})) {
sleep(1);
}
array_walk($Threads, function(Thread $Thread) {
var_dump($Thread->getSavedResult());
});
$res = (new ThreadPool(10)) //threads count
->setTask(function (Thread $Thread) {
$data = $Thread->getSavedData();
$i = isset($data['i']) ? $data['i'] : 0;
$i++;
var_dump($i);
$Thread->saveResult([
'i' => $i,
'num' => $Thread->getThreadNumber(),
]);
if ($i >= 10) {//script will exit after 10 iterations/launches
$Thread->markFinished();
}
})->setInterruptedHandler(function (Thread $Thread, $signo) {
//dump current state before exit
file_put_contents('worker_' . $Thread->getThreadNumber(), json_encode($Thread->getSavedResult()));
trigger_error('Interrupted with signal ' . $signo);
})->run();
var_dump($res);
// will return [
// 0 => [
// 'i' => 10,
// 'num' => 0
// ],
// 1 => [
// 'i' => 10,
// 'num' => 1
// ]
// ];