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