PTHREAD的安装

php下 pthread的安装需要重新编译php

以最新的 php5.67为例

wget http://am1.php.net/distributions/php-5.6.7.tar.gz

然后编译

复制代码
./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-curl --with-freetype-dir --with-jpeg-dir --with-png-dir --with-gd --enable-gd-native-ttf --with-iconv --with-libxml-dir --with-mhash --with-mcrypt --with-mysql --with-mysqli --with-openssl --with-xmlrpc --with-zlib --disable-debug --disable-rpath --enable-bcmath --enable-fpm --enable-inline-optimization --enable-mbregex --enable-mbstring --enable-pcntl --enable-shmop --enable-soap --enable-sockets --enable-sysvsem --enable-xml --enable-zip --with-libdir=lib64 --without-pear --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --enable-opcache --with-pdo-mysql --enable-maintainer-zts

cp php.ini-production /usr/local/php/etc/php.ini
复制代码

下载最新版的pthreads

wget https://github.com/krakjoe/pthreads/archive/v2.0.10.tar.gz
1 /usr/local/php/bin/phpize
2 ./configure --with-php-config=/usr/local/php/bin/php-config 
make && make install
/usr/local/php/lib/php/extensions/no-debug-zts-20131226/
extension = pthreads.so

 

案例

复制代码
<?php
    class test_thread_run extends Thread
    {
        public $url;
        public $data;
        
        public function __construct($url){
            $this->url = $url;
        }
        public function run(){
            if(($url = $this->url))
            {
                echo 'microtime:'.microtime();
                sleep(10);
                echo $this->url."\r\n";
            }
        }
    }
    
    for ($i=0; $i < 100; $i++){
        $urls_array[] = array("name" => "baidu", "url" => "http://www.baidu.com/s?wd=".mt_rand(10000,20000));
    }
    
    
    function model_thread_result_get($urls_array){
        foreach ($urls_array as $key => $value){
            $thread_array[$key] = new test_thread_run($value["url"]);
            $thread_array[$key]->start();
        }
        foreach ($thread_array as $thread_array_key => $thread_array_value){
            while($thread_array[$thread_array_key]->isRunning()){
                usleep(10);
            }
            if($thread_array[$thread_array_key]->join()){
                $variable_data[$thread_array_key] = $thread_array[$thread_array_key]->data;
            }
        }
        return $variable_data;
    }
    $result = model_thread_result_get($urls_array);
?>
复制代码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值