1. Go to this page and download the library: Download phpth/multi-process 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/ */
phpth / multi-process example snippets
use phpth\process\Process;
use phpth\process\supply\Call;
use phpth\process\supply\Status;
$p = new Process();
$data = [
'child data 1',
'child data 2',
'child data 3'
];
$que = Process::getIpcQueue('./queue.sv');
$que->setBlock(true);
// Supports all types except objects
$que->push([1]);
$que->push(null);
$que->push(false);
$que->push('');
$que->push('123');
$que->push('456');
$que->push('789');
$que->push('qwe');
$que->push('asd');
$que->push('zxc');
/*
//it will throw exception
$que->push(new class{})
$que->push([new class{}])
*/
//$que->push(new class{});
//$que->push(['sgsd']);
//$que->push([new class{}]);
$e = $p->runCall(function (int $processNo, mixed $param){
// child process execute: do something
// if you wang get parent process id, can do this
global $maiPid;
echo 'parent process id: '.$maiPid, PHP_EOL;
// callback function first param is process no, second param
echo "current process no: {$processNo}, process param: ".print_r($param).PHP_EOL;
sleep(1);
$que = Process::getIpcQueue('./queue.sv');
$que->setBlock(true);
$queueData = $que->pop();
echo 'get queue data: '.var_export($queueData, true).PHP_EOL;
},
$data,
Call::PROCESS_DATA_DISPATCH // Automatically start the process according to the number of array elements and split an element passed in $data into the second parameter of the callback function
);
foreach($e->wait(true, 1.5) as $processNo=> $status){
/**@var Status $status */
if(!$status->run){
echo $processNo.'stopped'.PHP_EOL;
}
}
// or
$p->waitExecutor($e);
// or
$p->waitExecutor($e, true ,1.9, function(int $idx, array $status, $append_param1, $append_param2){
// do log or other things
}, ['$append_param1', '$append_param2']);
use phpth\process\Process;
$p = new Process();
$p->name = 'process name'
// or
$p = new Process('process name');
use phpth\process\Process;
use phpth\process\supply\Options;
$p = new Process();
$e = $p->runCall(function ($a, $b){
pcntl_async_signals(true);
Options::registerSignal(SIGTERM, function($signal){
//your exit logic
});
// or
Options::registerSignal([SIGINT, SIGHUP], function($signal){
//your deal logic
});
// do something
sleep(1);
}, ['a', 'b'], 3);
$p->waitExecutor($e, true ,1.9, function(int $idx, array $status, $append_param1, $append_param2){
// do log or other things
}, ['$append_param1', '$append_param2']);
use phpth\process\Process;
use phpth\process\supply\Options;
$p = new Process();
// main process on normal finished, it will stop child process
$p->stopChildOnFinish = true;
// open your child process code