首页 > 后端开发 > php教程 > pthreads - 求助,php使用Pthread进行多进程的问题?

pthreads - 求助,php使用Pthread进行多进程的问题?

WBOY
发布: 2016-06-06 20:19:04
原创
1422 人浏览过

以下代码运行没有问题,感觉$urls数组有多少数据,就生产多少进程,如果有几万个数据,是不是会有几万个进程?

如果要设置成只有10个进程来处理$urls数组,应该怎么做?

<code><?php
    header("Content-Type: text/html;charset=utf-8");

    class DuoXianCheng extends Thread {
      public function __construct($arg){
        $this->arg = $arg;
      }
     
      public function run(){
        if($this->arg){
          // echo $this->result = $this->arg;
            $this->result = model_http_curl_get($this->arg);
        }
      }
    }

    $thread = new DuoXianCheng("World");
    if($thread->start()){
        $thread->join();
    }
      

    function model_http_curl_get($url) {
          $curl = curl_init();  
          curl_setopt($curl, CURLOPT_URL, $url);  
          curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);  
          curl_setopt($curl, CURLOPT_TIMEOUT, 5);  
          curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2)');  
          $result = curl_exec($curl);  
          curl_close($curl);  
          return $result;  
    }

    for ($i = 0; $i < 10; $i++) {
        $urls[] = 'http://www.baidu.com/s?wd='. rand(10000, 20000);
    }

/* 多线程速度测试 */
    $t = microtime(true);
    foreach ($urls as $key=>$url) {
        $workers[$key] = new \DuoXianCheng($url);
        $workers[$key]->start();
    }
    
    foreach ($workers as $key=>$worker) {
        while($workers[$key]->isRunning()) {
            usleep(100);  
        }
        if ($workers[$key]->join()) {
            var_dump($workers[$key]->result);
        }
    }
    $e = microtime(true);
    echo "多线程耗时:".($e-$t)."秒<br>";  
     
     
    /* 单线程速度测试 */
    $t = microtime(true);
    foreach ($urls as $key=>$url) {
        var_dump(model_http_curl_get($url));
    }
    $e = microtime(true);
    echo "For循环耗时:".($e-$t)."秒<br>";  



?></code>
登录后复制
登录后复制

回复内容:

以下代码运行没有问题,感觉$urls数组有多少数据,就生产多少进程,如果有几万个数据,是不是会有几万个进程?

如果要设置成只有10个进程来处理$urls数组,应该怎么做?

<code><?php
    header("Content-Type: text/html;charset=utf-8");

    class DuoXianCheng extends Thread {
      public function __construct($arg){
        $this->arg = $arg;
      }
     
      public function run(){
        if($this->arg){
          // echo $this->result = $this->arg;
            $this->result = model_http_curl_get($this->arg);
        }
      }
    }

    $thread = new DuoXianCheng("World");
    if($thread->start()){
        $thread->join();
    }
      

    function model_http_curl_get($url) {
          $curl = curl_init();  
          curl_setopt($curl, CURLOPT_URL, $url);  
          curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);  
          curl_setopt($curl, CURLOPT_TIMEOUT, 5);  
          curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2)');  
          $result = curl_exec($curl);  
          curl_close($curl);  
          return $result;  
    }

    for ($i = 0; $i < 10; $i++) {
        $urls[] = 'http://www.baidu.com/s?wd='. rand(10000, 20000);
    }

/* 多线程速度测试 */
    $t = microtime(true);
    foreach ($urls as $key=>$url) {
        $workers[$key] = new \DuoXianCheng($url);
        $workers[$key]->start();
    }
    
    foreach ($workers as $key=>$worker) {
        while($workers[$key]->isRunning()) {
            usleep(100);  
        }
        if ($workers[$key]->join()) {
            var_dump($workers[$key]->result);
        }
    }
    $e = microtime(true);
    echo "多线程耗时:".($e-$t)."秒<br>";  
     
     
    /* 单线程速度测试 */
    $t = microtime(true);
    foreach ($urls as $key=>$url) {
        var_dump(model_http_curl_get($url));
    }
    $e = microtime(true);
    echo "For循环耗时:".($e-$t)."秒<br>";  



?></code>
登录后复制
登录后复制

curl_init()不开新进程,但客户机的file descriptor是有限的,内存也是有限的,意味着能new的Thread不是无限的。你要达到pthread要达到的目的,不妨看看http://php.net/manual/en/function.curl-multi-init.php,它允许你异步的处理多个curl_init()返回。

Thread 类是线程,不是进程。
创建十个 DuoXianCheng 的对象:
for ($i = 0 ; $i <=9 ; $i ) {
$obj[$i] = new DuoXianCheng("World");
$obj[$i]->start();
}

感谢大家的回复!我指的是线程,不是进程,打字打错了!
如果$urls有十万条数据,然后只开十个线程,应该怎么操作?

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板