通俗易懂的php多線程解決方案

咱們在作項目的時候,有些需求,特別是數據的響應處理須要花費大量的時間,因爲php是一個短生命週期的腳本語言,到了默認的30秒,php的數據處理還沒完成,php的生命週期就結束了。這時須要使用異步併發處理策略,也就是說,一次php調用能夠發出的多個請求,這些請求不是按照順序執行,而是能夠異步併發執行的,一些請求用於在後臺處理數據,一些請求用於接受後臺響應狀態,根據狀態,與用戶作一些簡單的交互。可是問題來了,咱們都知道php自己是不支持多線程的,那麼應該怎麼實現php的多線程呢?php

1、php模擬實現多線程的三種方法html

一、linux下的php多線程mysql

下面所講的東西是源自php的pcntl_fork函數.由於這個函數依賴操做系統fork的實現,因此本文所講的東西只適用於linux/unix。那麼先看看這個函數的用法吧.php手冊上是這麼說的:linux

<?php
$pid = pcntl_fork();
if ($pid == -1) {
         die('could not fork');
} else if ($pid) {
         // we are the parent
         pcntl_wait($status); //Protect against Zombie children
} else {
         // we are the child
}
?>

經過pcntl_fork建立一個子進程,若是返回值是-1的話,那麼說明子進程建立失敗.建立成功的進程id會返回給父進程,0返回給子進程.很差理解吧,因此應該這樣寫:git

<?php
$pid = pcntl_fork();
if($pid == -1){
         //建立失敗咱就退出唄,沒啥好說的
         die('could not fork');
}
else{
        if($pid){
                //從這裏開始寫的代碼是父進程的,由於寫的是系統程序,記得退出的時候給個返回值
                exit(0);
        }
        else{
                //從這裏開始寫的代碼都是在新的進程裏執行的,一樣正常退出的話,最好也給一個返回值
                exit(0);
        }
}
?>

這樣一改好理解多了,若是你父進程但願知道子進程正常退出的話,能夠加上前面的pcntl_wait。github

2.經過stream_socket_client 方式sql

function sendStream() { 
    $english_format_number = number_format($number, 4, '.', ''); 
  
    echo $english_format_number;  
    exit(); 
    $timeout = 10; 
    $result = array(); 
    $sockets = array(); 
    $convenient_read_block = 8192; 
    $host = "test.local.com"; 
    $sql = "select waybill_id,order_id from xm_waybill where status>40 order by update_time desc limit 1 ";  
    $data = Yii::app()->db->createCommand($sql)->queryAll(); 
    $id = 0; 
  
    foreach ($data as $k => $v) { 
      if ($k % 2 == 0) { 
        $send_data[$k]['body'] = NoticeOrder::getSendData($v['waybill_id']); 
  
      } else { 
        $send_data[$k]['body'] = array($v['order_id'] => array('extra' => 16));  
      }  
      $data = json_encode($send_data[$k]['body']); 
      $s = stream_socket_client($host . ":80", $errno, $errstr, $timeout, STREAM_CLIENT_ASYNC_CONNECT | STREAM_CLIENT_CONNECT); 
      if ($s) {  
        $sockets[$id++] = $s; 
        $http_message = "GET /php/test.php?data=" . $data . " HTTP/1.0\r\nHost:" . $host . "\r\n\r\n";  
        fwrite($s, $http_message); 
      } else {  
        echo "Stream " . $id . " failed to open correctly."; 
      }  
    } 
  
    while (count($sockets)) { 
  
      $read = $sockets; 
  
      stream_select($read, $w = null, $e = null, $timeout); 
       if (count($read)) {  
        /* stream_select generally shuffles $read, so we need to 
         compute from which socket(s) we're reading. */
        foreach ($read as $r) { 
  
          $id = array_search($r, $sockets); 
          $data = fread($r, $convenient_read_block); 
          if (strlen($data) == 0) { 
            echo "Stream " . $id . " closes at " . date('h:i:s') . ".<br>  "; 
            fclose($r); 
             unset($sockets[$id]); 
          } else { 
            $result[$id] = $data; 
          } 
        } 
      } else {  
        /* A time-out means that *all* streams have failed 
         to receive a response. */
        echo "Time-out!\n"; 
        break; 
      }  
    }  
    print_r($result); 
  
  }

3、經過多進程代替多線程
function daemon($func_name,$args,$number){ 
  while(true){ 
    $pid=pcntl_fork(); 
    if($pid==-1){ 
      echo "fork process fail"; 
      exit(); 
    }elseif($pid){//建立的子進程 
  
      static $num=0; 
      $num++; 
      if($num>=$number){ 
        //當進程數量達到必定數量時候,就對子進程進行回收。 
        pcntl_wait($status); 
  
        $num--; 
      }  
    }else{ //爲0 則表明是子進程建立的,則直接進入工做狀態 
  
      if(function_exists($func_name)){ 
        while (true) { 
          $ppid=posix_getpid(); 
          var_dump($ppid); 
          call_user_func_array($func_name,$args); 
          sleep(2); 
        } 
      }else{ 
        echo "function is not exists"; 
      } 
      exit();   
    } 
  } 
}  
function worker($args){  
  //do something 
  
}  
daemon('worker',array(1),2); 

2、真正實現php多線程的方法

php真正的多線程實現方式,經過安裝php的擴展 pthread 能夠作到。

 
可是這個下載的是 版本3 也就是php 7 才能用的,咱們須要使的是 版本2
而後刷新的頁面以下,拖到最底部:

下一頁找到版本2的json

下載下來,這個v2 纔是php5纔可使用的vim

下載下來,安裝:多線程

或者,您直接這樣下載:

cd /tools  
   wget https://github.com/krakjoe/pthreads/archive/v2.0.10.zip  
   unzip   v2.0.10.zip  
   cd pthreads-2.0.10  
   /usr/local/php/bin/phpize  
   ./configure --with-php-config=/usr/local/php/bin/php-config    
   make  
   make install

注意:您的php 在編譯的時候須要開啓 –enable-maintainer-zts

./configure --prefix=/usr/local/php --disable-fileinfo --enable-fpm --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-openssl --with-zlib --with-curl --enable-ftp --with-gd --with-xmlrpc --with-jpeg-dir --with-png-dir --with-freetype-dir --enable-gd-native-ttf --enable-mbstring --with-mcrypt=/usr/local/libmcrypt --enable-zip --with-mysql=/usr/local/mysql --without-pear --enable-maintainer-zts

vim /etc/php.ini 
添加
extension=pthreads.so

  重啓php /etc/init.d/php-fpm restart

 

https://www.w3cschool.cn/php/php-thread.html

相關文章
相關標籤/搜索