可直接拿去使用,前提是Linux系统下安装了pcntl扩展
// 开启了100个进程
for ($i = 0; $i < 100; $i++) {
$pid = pcntl_fork();
if ($pid == -1) {
echo "fork错误";
} elseif ($pid == 0) {
// 下面代码可以换成其他的执行代码
$command = "php forkFixMission.php --shard={$i} 2>&1 >stdout.log &";
shell_exec($command);
return;
} else {
$childProcess[] = $pid;
}
}
while (count($childProcess) > 0) {
foreach ($childProcess as $key => $pid) {
$res = pcntl_waitpid($pid, $status, WNOHANG);
if ($res == -1 || $res > 0) {
unset($childProcess[$key]);
}
}
sleep(1);
}