PHP多进程实践 发表于 2016-09-13 利用pcntl_fork实现PHP多进程代码如下: 12345678910111213141516171819202122232425262728293031323334function runTasks($tasks) { $linenum = 2; $count = count($tasks); if ($count > 30) { $linenum = (int)($count/15); } //任务分割 $chunks = array_chunk($tasks, $linenum); $procs = array(); foreach ($chunks as $chunk) { $pid = pcntl_fork(); if ($pid == -1) { die('could not fork'); } else if ($pid) { $procs[] = $pid; } else { foreach ($chunk as $line) { system('php '. $line); echo $line." done!\n"; } exit(0); } } foreach ($procs as $proc) { pcntl_waitpid($proc, $status); } unset($pid); $pid = NULL; unset($procs); $procs = NULL; unset($chunks); $chunks = NULL; } 部署脚本: 123456789101112131415161718#!/bin/bashsource /etc/profilesource /root/.bash_profilenowDate=`date +%Y%m%d`cd /yourpathnowTime=`date +%M`//每小时一次 01分的时候if [ "$nowTime" -eq "01" ]then ProcessCount=`ps -ef|grep "tash.php" |grep -v "grep"|wc -l` if [ $ProcessCount -lt 1 ] then nohup /usr/bin/php test.php >> /dev/null & echo "Create test.php run!" fifi 多谢打赏 坚持原创技术分享,您的支持将鼓励我继续创作! 赏 微信打赏 支付宝打赏