PHP Multi-process work

Source: Internet
Author: User
Tags rekey

background : Daily Nine o'clock in the morning push, user 340,000 people

Technical Bottleneck : PHP is a weak type of language, from the beginning to the end, causing the push to grow 2 hours longer

demand : Reduce push time, limited to half an hour

After the introduction of the background, start the topic:

Put data into Redis, extract data directly from MySQL without optimization, use Popen to open the process pointer to execute the corresponding logic, some code, code dependencies are not all given,

1 $redis= Rediscache ();//Link Redis2 $rekey=Key’;//here to use the method to generate a unique key3 if($redis->exists ($rekey) ) {4        $redis->del ($rekey);5 }6.7.8 //The data extraction process.9.Ten. One. A $data=Array(//Data extraction style -' User ' =Array($v[' userid ']), -' Describe ' + ' smile punch-in reminder '.Ceil($day) . ' Day ', the' Thing ' = ' Smile punch ', -' Time ' =Date(' Y-m-d h:i ',Mktime(9, 0, 0,Date(' m '),Date(' d '),Date(' Y ')), -' url ' = =$jump, -' Epiogue ' + ' click upload smile photo > ', +             ); - $redis->lpush ($rekey,Serialize($data));//Serialize into List + //Set key expiration time, timely return server resources A $length=$redis->llen ($rekey); at $cell= 10000;//the approximate number of pushes per hour - if($redis->exists ($rekey) && $length >0) { -             $index=$length/$cell; -             if(intval($index) > 0 ) { -                 $redis->expire ($rekey,intval($index) *3600); -}Else { in                 $redis->expire ($rekey, 3600); -       } to } + //for multi-threaded push - Switch(intval($length)) { the      Case $length<200:{//a process *         Pclose(Popen("PHP./index.php-a 0-b". ($length-1). "&", ' R ')); $        $return=true;Panax Notoginseng         Break; -     } the      Case $length>= 200:{//Two processes +         if($length%2 = = 0) {//is even A             $nextStart=$length/2; the}Else{//is odd +             $nextStart= Floor($length/2); -         } $  $          Pclose(Popen("PHP./index.php-a 0-b". ($nextStart). "&", ' R ') ;//To release process resources after use -         Pclose(Popen("PHP./index.php-a". ($nextStart+1). "-B". ($length-1). "&", ' R ') ;//release of integrated resources after use -         $return=true; the          Break; -      }Wuyi      default: the          $return=false; -           Break; Wu} -return $return;

Popen: Opens a pipeline that points to a process that is generated by the execution of the specified command command. Remember to release process resources after use

PHP Multi-process work

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.