PHP多进程开发与Redis结合实践

原文:https://blog.51cto.com/laok8/2107892?source=drh

业务逻辑介绍:

  1. 用户在 APP 上发帖子,然后存储到 Redis 的 List 列表中
  2. 利用 Linux 的 crontab 定时任务功能,按秒请求执行PHP脚本文件(processNewsRedisList.php)
  3. 调用 redis_process 处理API,进行存储到 Mysql 中

1.发帖子API

public function post_json() {

        $image = $_FILES['image'];
        $data = I();

        $images = $this->post_upload($image);
        $data['image'] = $images ? $images : '';

        if( count($data) ){
            $redis = new Redis();
            $data['creation_time'] = time();
            //把发过来的帖子存储redis
            $result = $redis->lpush('news_list', json_encode($data));
//          $redis->lpush('news_list', json_encode($data));
//          while (TRUE){
//              if ($redis->lsize('news_list') > 0){
//                  $info = $redis->rpop('news_list');
//                  $info = json_decode($info, true);

//                  $result = $this->_model->news_publish( $info );
//              }else {
//                  sleep(1);break;
//              }
//          }
//          $redis->close();
            $data['creation_time'] = time();

//          $result = $this->_model->news_publish( $data );

            if( $result < 1 ){//redis方式
//          if( !$result ){//model方式

                $this->_data['data'] = '';
                $this->_data['error'] = true;
                $this->_data['message'] = L('_DATABASE_ERROR_');

            }else{//success

                $this->_data['data'] = $result;
                $this->_data['error'] = false;
            }

        }
        else{
            $this->_data['data'] = '';
            $this->_data['error'] = true;
            $this->_data['message'] = L('_Invalid_Parameters_');
        }

        $this->response($this->_data);
    }

 
 

2.processNewsRedisList.php

<?php
/**
*检查队列中帖子,并把帖子插入数据库表中
*/

function worker()
{
        //再次克隆出子进程
        $pid = pcntl_fork();
        if ( $pid == -1 ) {
                exit('fork error');
        }
        if ( $pid == 0 ) {
                $redis = new Redis();
                $redis->connect('127.0.0.1', 7200);
                //业务逻辑代码
                while(true) {//sleep(5);
                        if( $redis->lsize('news_list') > 0 ){
                                $info = $redis->rpop('news_list');
                                $info = json_decode($info, TRUE);
                                //使用curl调用API接口
                                $uri = "http://*.*.*.*:*/api.zmartec/News/redis_process";

                                $data = $info;
                                $ch = curl_init();
                                curl_setopt($ch, CURLOPT_URL, $uri);
                                curl_setopt($ch, CURLOPT_POST, 1);
                                curl_setopt($ch, CURLOPT_HEADER, 0);
                                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                                curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

                                $result = curl_exec($ch);
                                curl_close($ch);
                        } else {
                                sleep(1);//队列中没有任务的时候,睡眠1s,让出cpu给其它进程
                        }
                }
                $redis->close();
        }
}

/**
*子进程,负责设置子进程为领导小组
*
*并调用处理函数,处理任务
*/
function children()
{
        $sid = posix_setsid();
        echo $sid;
        for( $i = 0; $i < 2; $i++ ) {
                worker();
        }
}

//克隆子进程,返回子进程的进程id
$pid = pcntl_fork();
if ( $pid == -1 ) {
        exit('fork error');
}

if ( $pid == 0 ) {
        children();
}else{
        exit('parent exit');
}

?>

 
 

3.存储帖子到 Mysql

thinkphp 3.2框架:
public function redis_process()
{
        $data = $_POST;
        if ($this->_model->news_publish($data)) {
                    return true;
                } esle {
            return false;
              }
    }

public function news_publish( $data )
{

        try{
            return M('news')->add($data);
        }catch(Exception $ex){
            return FALSE;   
        }   
    }
原文地址:https://www.cnblogs.com/lxwphp/p/11134045.html