搜索
首页php教程php手册php实现类多线程的方法总结

php实现类多线程的方法总结

Jun 13, 2016 am 10:58 AM
php不支持实现总结方法线程需要

家都很清楚,php是不支持多线程的。但对于需要类似多线程功能的人来说确实是个头疼的问题。好在有几种方案可以进行解决,类似多线程功能。下面是本人总结的三种实现多线程类似的方法的方案,下面是三种方案与代码实例。

1、curl_multi方法

当需要多线程的时候,可以用curl_multi一次性请求多个操作来完成,但curl走的是网络通信,效率与可靠性就比较差了的。

 

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; 
 
  
 
   } 

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); 
 
    } 

function sendStream() {

 

 

 

        $english_format_number = number_format($number, 4, &#39;.&#39;, &#39;&#39;);

        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][&#39;body&#39;] = NoticeOrder::getSendData($v[&#39;waybill_id&#39;]);

            } else {

 

                $send_data[$k][&#39;body&#39;] = array($v[&#39;order_id&#39;] => array(&#39;extra&#39; => 16));

            }

            $data = json_encode($send_data[$k][&#39;body&#39;]);

 

            $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&#39;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); 

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);
 
 

 

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
4 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
4 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
4 周前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解锁Myrise中的所有内容
1 个月前By尊渡假赌尊渡假赌尊渡假赌

热工具

VSCode Windows 64位 下载

VSCode Windows 64位 下载

微软推出的免费、功能强大的一款IDE编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

Dreamweaver Mac版

Dreamweaver Mac版

视觉化网页开发工具

mPDF

mPDF

mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

Atom编辑器mac版下载

Atom编辑器mac版下载

最流行的的开源编辑器