zoukankan      html  css  js  c++  java
  • windows下安装php真正的多线程扩展pthreads教程

    扩展地址:http://docs.php.net/manual/zh/book.pthreads.php

    注意事项
    php5.3或以上,且为线程安全版本。apache和php使用的编译器必须一致。
    通过phpinfo()查看Thread Safety为enabled则为线程安全版。
    通过phpinfo()查看Compiler项可以知道使用的编译器。本人的为:MSVC9 (Visual C++ 2008)。


    本人使用环境
    32位windows xp sp3,wampserver2.2d(php5.3.10-vc9 + apache2.2.21-vc9)。


    一、下载pthreads扩展
    下载地址:http://windows.php.net/downloads/pecl/releases/pthreads
    根据本人环境,我下载的是pthreads-2.0.8-5.3-ts-vc9-x86。
    2.0.8代表pthreads的版本。
    5.3代表php的版本。
    ts表示php要线程安全版本的。
    vc9表示php要Visual C++ 2008编译器编译的。
    x86则表示32位的


    二、安装pthreads扩展
    复制php_pthreads.dll 到目录 binphpext 下面。(本人路径D:wampinphpphp5.3.10ext)
    复制pthreadVC2.dll 到目录 binphp 下面。(本人路径D:wampinphpphp5.3.10)
    复制pthreadVC2.dll 到目录 C:windowssystem32 下面。
    打开php配置文件php.ini。在后面加上extension=php_pthreads.dll
    提示!Windows系统需要将 pthreadVC2.dll 所在路径加入到 PATH 环境变量中。我的电脑--->鼠标右键--->属性--->高级--->环境变量--->系统变量--->找到名称为Path的--->编辑--->在变量值最后面加上pthreadVC2.dll的完整路径(本人的为C:WINDOWSsystem32pthreadVC2.dll)。


    三、测试pthreads扩展

    class AsyncOperation extends Thread {
        public function __construct($arg){
            $this->arg = $arg;
        }
        public function run(){
            if($this->arg){
                printf("Hello %s
    ", $this->arg);
            }
        }
     }
    $thread = new AsyncOperation("World");
     if($thread->start())
        $thread->join();
     ?>

    运行以上代码出现 Hello World,说明pthreads扩展安装成功! 

    附上一个Thinkphp3.2.2简单例子

    <?php
     namespace HomeController;
     class test extends Thread {
        public $url;
        public $result;
        
        public function __construct($url) {
            $this->url = $url;
        }
        
        public function run() {
            if ($this->url) {
                $this->result = model_http_curl_get($this->url);
            }
        }
     }
     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 test($url);
        $workers[$key]->start();
     }
     foreach ($workers as $key=>$worker) {
        while($workers[$key]->isRunning()) {
            usleep(100);  
        }
        if ($workers[$key]->join()) {
            dump($workers[$key]->result);
        }
     }
    $e = microtime(true);
    echo "多线程耗时:".($e-$t)."秒<br>";  
     /* 单线程速度测试 */
    $t = microtime(true);
     foreach ($urls as $key=>$url) {
        dump(model_http_curl_get($url));
     }
    $e = microtime(true);
    echo "For循环耗时:".($e-$t)."秒<br>";  

    测试结果如下:
    多线程耗时:2.8371710777282714844秒
    For循环耗时:10.941586017608642578秒

    原文出自:http://www.thinkphp.cn/topic/22676.html

  • 相关阅读:
    ASP.NET AJAX Beta 1 发布 (转载)
    ASP.NET里常用的JS (转贴)
    让您的Ajax应用加载数据时界面友好 (转贴)
    模态窗口 javascript html
    最亲密接触Dhtml&JScript开发细节 (转贴)
    Hashtable的使用
    2009年全国年节及纪念日放假办法
    详解.NET中的动态编译
    CSS2.0样式手册_说明_SDK下载chm
    [转]DISTINCT 和 ORDER BY 使用第三个字段进行排序
  • 原文地址:https://www.cnblogs.com/q1ng/p/4657132.html
Copyright © 2011-2022 走看看