"Four" PHP multi-process development and Redis integration practice

Source: Internet
Author: User
Tags php script

Introduction to Business logic:
    1. The user posts on the APP and then stores it in the Redis list
    2. Execute php script file (processnewsredislist.php) in seconds request with Linux crontab Timer task function
    3. Call the Redis_process processing API to store it in Mysql

1. Posting Sub 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 (); Store the posted posts for 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);//$re              Sult = $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 mode//if (! $rEsult) {//model mode $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/*** Check the posts in the queue and insert the post into the database table */function Worker () {//clone the child process again $pid = Pcntl_fork ();        if ($pid = =-1) {exit (' fork error ');                } if ($pid = = 0) {$redis = new Redis ();                $redis->connect (' 127.0.0.1 ', 7200);                        Business logic code while (true) {//sleep (5);                                if ($redis->lsize (' news_list ') > 0) {$info = $redis->rpop (' news_list ');                                $info = Json_decode ($info, TRUE);                                Use Curl to invoke API interface $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);//When there is no task in the queue, sleeping 1s, yielding cpu to other processes}}        $redis->close ();        }}/*** child process, responsible for setting child process to lead group * * and call handler function, processing Task */function children () {$sid = Posix_setsid ();        Echo $sid;        for ($i = 0; $i < 2; $i + +) {worker (); }}//clones the subprocess, returns the process of the subprocess Id$pid = Pcntl_fork (), if ($pid = =-1) {exit (' fork error ');} if ($pid = = 0) {children ();} Else{exit (' parent exit '); >


3. Store posts to Mysql
thinkphp 3.2框架:public function redis_process(){        $data = $_POST;        $this->_model->news_publish($data);        return true;    }public function news_publish( $data ){        try{            return M(‘news‘)->add($data);        }catch(Exception $ex){            return FALSE;           }       }





Please note, thank you for your lack of error.

"Four" PHP multi-process development and Redis integration practice

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.