currently in the project, you want to use php"s pcntl_fork to create multiple processes (such as 8) to consume redis queue messages in parallel. But running the log found that there were not eight processes generated, but two or three. As shown below:
//8
for ($i = 0; $i < 8; $iPP) {
//fork
$pid = pcntl_fork();
Logger::info("************ forked process id=" . $pid);
if ($pid == 0) {
$cid = $pid;
$pid = posix_getpid();
$ppid = posix_getppid();
$mypid = getmypid(); //pid mypidId
Logger::info("************ child process cid=" . $cid . "|pid=" . $pid . "|ppid=" . $ppid . "|mypid=" . $mypid);
//redis
$this->doWork();
Logger::info("************ work finished by pid=" . $pid);
exit(0);
}
}
private function doWork()
{
//
while (true) {
//pop redis msg
//...
}
}
Log print shows that it has been run 8 times
************ child process cid=0|pid=107070|ppid=107069|mypid=107070
************ child process cid=0|pid=107071|ppid=107069|mypid=107071
************ child process cid=0|pid=107072|ppid=107069|mypid=107072
************ child process cid=0|pid=107073|ppid=107069|mypid=107073
************ child process cid=0|pid=107074|ppid=107069|mypid=107074
************ child process cid=0|pid=107074|ppid=107069|mypid=107075
************ child process cid=0|pid=107074|ppid=107069|mypid=107076
************ child process cid=0|pid=107074|ppid=107069|mypid=107077
but in fact, the number of processes checked through ps aux | grep php is not 8 (only 3 are 107071, 107072, 107073):
23156 107071 0.0 0.0 112116 6832 pts/1 S 18:25 0:00 /usr/local/php/bin/php -f client.php
23156 107072 0.0 0.0 112116 6832 pts/1 S 18:25 0:00 /usr/local/php/bin/php -f client.php
23156 107073 0.0 0.0 112116 6848 pts/1 S 18:25 0:00 /usr/local/php/bin/php -f client.php
what"s wrong with this? How to correct it? Thank you!