>  기사  >  백엔드 개발  >  PHP에서 멀티스레딩을 구현하는 세 가지 유사한 방법

PHP에서 멀티스레딩을 구현하는 세 가지 유사한 방법

高洛峰
高洛峰원래의
2016-12-21 13:06:26865검색

1. Curl_multi 방식

멀티스레딩이 필요한 경우, 컬_멀티를 사용하면 한 번에 여러 작업을 요청할 수 있지만, 컬은 네트워크 통신을 사용하므로 효율성과 안정성이 상대적으로 떨어집니다.

function main(){
  
   $sql = "select waybill_id,order_id from waybill where status>40 order by update_time desc limit 10 ";
  
    $data = Yii::app()->db->createCommand($sql)->queryAll(); //yii 框架格式
  
    foreach ($data as $k => $v) { 
  
      if ($k % 2 == 0) { //偶数发一个网址
  
        $send_data[$k]['url'] = '';
  
        $send_data[$k]['body'] = $v['waybill_id'];
  
      } else { //奇数发送另外一个网址
        $send_data[$k]['url'] = 'http://www.abc.com';
  
        $send_data[$k]['body']=array($v['order_id'] => array('extra' => 16));
  
      }
  
    }
 
    $back_data =sendMulitRequest($send_data);
  
    var_dump($back_data);
  
  }
  function sendMulitRequest($send_data){
    $params = array();
    $curl = $text = array();
    $handle = curl_multi_init();
  
    foreach ($data as $k => $v) {
  
      if (empty($v['url'])) {
  
        $v['url'] = "http://www.xxx.com"; //if url is empty,set defalut url
  
      }
  
      $reqBody = json_encode($v['body']);
  
      $reqStream = array(
  
        'body' => $reqBody,
      ); 
      $encRequest = base64_encode(json_encode($reqStream)); 
      $params['data'] = $encRequest;
      $curl[$k] = curl_init();
      curl_setopt($curl[$k], CURLOPT_URL, $v['url']);
      curl_setopt($curl[$k], CURLOPT_POST, TRUE);
      curl_setopt($curl[$k], CURLOPT_HEADER, 0);
      curl_setopt($curl[$k], CURLOPT_POSTFIELDS, http_build_query($params));
      curl_setopt($curl[$k], CURLOPT_RETURNTRANSFER, 1);
      curl_multi_add_handle($handle, $curl[$k]);
    }
    $active = null;
  
    do {
  
      $mrc = curl_multi_exec($handle, $active);
  
    } while ($mrc == CURLM_CALL_MULTI_PERFORM);
    while ($active && $mrc == CURLM_OK) {
  
      if (curl_multi_select($handle) != -1) {
  
        do {
  
          $mrc = curl_multi_exec($handle, $active);
  
        } while ($mrc == CURLM_CALL_MULTI_PERFORM);
  
      }
  
    }
    foreach ($curl as $k => $v) { 
      if (curl_error($curl[$k]) == "") {
        $text[$k] = (string) curl_multi_getcontent($curl[$k]);
       } 
      curl_multi_remove_handle($handle, $curl[$k]); 
      curl_close($curl[$k]);
    }
    curl_multi_close($handle); 
    return $text; 
  }

2. stream_socket_client 메소드를 통해

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(&#39;h:i:s&#39;) . ".<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(&#39;worker&#39;,array(1),2);

위는 PHP에서 멀티스레딩을 구현하는 세 가지 유사한 방법을 공유해 드리겠습니다. 학습에 도움이 됩니다.

PHP에서 멀티스레딩을 구현하는 세 가지 유사한 방법에 대한 더 많은 관련 기사를 보려면 PHP 중국어 웹사이트에 주목하세요!


성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.