实现真php多线程pthread


多线程是什么
是在在一个进程中并发多个线程,每条线程并行执行不同的任务。
多线程提高程序的执行效率,多线程比单线程被操作系统调度的概率更大,且高效。
多个线程可在多核CPU的多个核心同时运行,加快运行效率,线程间通信更简单
pthreads 官方文档 https://www.php.net/manual/zh/book.pthreads.php
PHP多线程
PHP默认不支持多线程,多线程需要安装扩展, PHP 5.3 以上版本可使用 pthreads PHP扩展,
扩展有pcnlt,POSIX,pthreads,
最多的是pthreads,通过参数指定编译PHP线程安全方式
用线程要考虑线程安全,
指某个函数在多线程环境调用时,正确的处理多线程间的共享变量,使程序功能能正确完成
POSIX线程POSIX threads简称Pthreads,是线程的POSIX标准,该标准定义了创建和操纵线程的一整套API。
类Unix操作系统Unix、Linux、Mac OS X等都使用Pthreads作为操作系统的线程
windows pthread安装
https://windows.php.net/downloads/pecl/releases/pthreads/
pthreads扩展安装步骤
查看phpinfo,获取PHP版本号及位数 x86表示32位,x64表示64位 编译器版本
PHP配置文件加载所在位置等
windows pthreads扩展下载地址:http://windows.php.net/downloads/pecl/releases/pthreads/
在扩展列表中找到你的系统的对应版本
php_pthreads-2.0.9-5.5-ts-vc11-x86.zip参数详解
2.0.9代表pthreads的版本号
5.5代表php的版本号
ts表示php是线程安全版本
vc11表示php要MSVC11 (Visual C++ 2012)编译器编译
x86则表示PHP版本是32位
解压缩包复制
php_pthreads.dll到D:\wamp\bin\php\php5.5.12\ext\目录下
pthreadVC2.dll到D:\wamp\bin\php\php5.5.12\目录下
pthreadVC2.dll到D:\wamp\bin\apache\apache2.4.9\bin目录下
pthreadVC2.dll到C:\windows\system32目录下
打开phpinfo()中Loaded Configuration File所指定加载的
php.ini配置文件并在末尾添加
extension=php_pthreads.dll
重启wamp/xampp服务器,重启服务器后,查看phpinfo() 看到 pthreads 表示安装成功
linux pthread安装
版本3是php7用的  https://github.com/krakjoe/pthreads
版本2 是php5用的 https://github.com/krakjoe/pthreads/releases
cd /tools  
wget https://github.com/krakjoe/pthreads/archive/v2.0.10.zip  
unzip   v2.0.10.zip  
cd pthreads-2.0.10  
/usr/local/php/bin/phpize  
./configure --with-php-config=/usr/local/php/bin/php-config    
make  
make install
您的php编译的时候需要开启 –enable-maintainer-zts
扩展的编译安装 Linux 编辑参数 –enable-maintainer-zts 是必选项
./configure --prefix=/usr/local/php --disable-fileinfo   --enable-fpm --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-openssl --with-zlib --with-curl --enable-ftp --with-gd --with-xmlrpc  --with-jpeg-dir --with-png-dir --with-freetype-dir --enable-gd-native-ttf --enable-mbstring --with-mcrypt=/usr/local/libmcrypt --enable-zip --with-mysql=/usr/local/mysql --without-pear --enable-maintainer-zts
vim /etc/php.ini
添加 extension=pthreads.so
重启php  
/etc/init.d/php-fpm restart
PHP多线程与For循环 抓取百度搜索页面的PHP代码
<?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)){
            $this->data = model_http_curl_get($url);
        }
    }
}
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;
}
function model_http_curl_get($url,$userAgent=""){
    $userAgent = $userAgent ? $userAgent : 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2)';
    $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, $userAgent);
    $result = curl_exec($curl);
    curl_close($curl);
    return $result;
}
for ($i=0; $i < 100; $i++){
    $urls_array[] = array("name" => "baidu", "url" => "http://www.baidu.com/s?wd=".mt_rand(10000,20000));
}
$t = microtime(true);
$result = model_thread_result_get($urls_array);
$e = microtime(true);
echo "多线程:".($e-$t)."";

$t = microtime(true);
foreach ($urls_array as $key => $value)    {
    $result_new[$key] = model_http_curl_get($value["url"]);
}
$e = microtime(true);
echo "For循环:".($e-$t)."";

//多线程:1.4300811290741
//For循环:9.3985378742218
//可以看出速度有10倍差距