首页 > 代码库 > php多进程工作

php多进程工作

背景:每天早晨九点进行微信推送,用户3-4万人

技术瓶颈:php为弱类型的语言,从头执行到尾,导致推送时长长大2个多小时

需求:减少推送时间,限定在半个小时

背景介绍完毕,开始正题:

将数据放入redis中,没有优化前直接从mysql中提取数据,使用popen开启进程指针执行相应逻辑,一下为部分代码,代码依赖关系没有全部给出,

 1 $redis = rediscache();//链接redis
 2 $rekey =‘key’;//在这里要使用方法生成唯一key
 3 if ( $redis->exists($rekey) ) {
 4        $redis->del($rekey);
 5 }
 6 .
 7 .
 8 //数据提取过程.
 9 .
10 .
11 .
12 $data = array(//数据提取样式
13                 ‘user‘ => array($v[‘userid‘]),
14                 ‘describe‘ => ‘微笑打卡提醒 第‘ . ceil($day) . ‘天‘,
15                 ‘thing‘    => ‘微笑打卡‘,
16                 ‘time‘     => date(‘Y-m-d H:i‘, mktime(9, 0, 0, date(‘m‘), date(‘d‘), date(‘Y‘))),
17                 ‘url‘      => $jump,
18                 ‘epiogue‘  => ‘点击上传微笑照片>‘,
19             );
20 $redis->lPush($rekey,serialize($data));//序列化存进列表中
21 //设置key的过期时间,及时的归还服务器资源
22 $length = $redis->lLen($rekey);
23 $cell = 10000;//微信每个小时推送大概的数量
24 if ( $redis->exists($rekey) && $length>0 ) {
25             $index = $length/$cell;
26             if ( intval($index) > 0 ) {
27                 $redis->expire($rekey,intval($index)*3600);
28             } else {
29                 $redis->expire($rekey,3600);
30       }
31 }
32 //进行多线程推送
33 switch (intval($length)) {
34   case $length<200:{//一个进程
35       pclose(popen("php ./index.php -a 0 -b ".($length-1)." &",‘r‘));
36        $return = true;
37        break;
38   }
39     case $length >= 200:{//两个进程
40         if ( $length%2 == 0 ) {//是偶数
41           $nextStart = $length/2;
42         } else {//是奇数
43           $nextStart = floor($length/2);
44         }
45 
46        pclose(popen("php ./index.php -a 0 -b ".($nextStart)." &",‘r‘));//使用后要释放进程资源
47         pclose(popen("php ./index.php -a ".($nextStart+1)." -b ".($length-1)." &",‘r‘));//使用后释放集成资源
48         $return = true;
49         break;
50      }
51      default:
52        $return = false;
53          break;
54}
55  return $return;

 popen:打开一个指向进程的管道,该进程由派生指定的 command 命令执行而产生。使用后记得释放进程资源

php多进程工作