扩展地址: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 到目录 bin\php\ext\ 下面。(本人路径D:\wamp\bin\php\php5.3.10\ext)
复制pthreadVC2.dll 到目录 bin\php\ 下面。(本人路径D:\wamp\bin\php\php5.3.10)
复制pthreadVC2.dll 到目录 C:\windows\system32 下面。
打开php配置文件php.ini。在后面加上extension=php_pthreads.dll
提示!Windows系统需要将 pthreadVC2.dll 所在路径加入到 PATH 环境变量中。我的电脑--->鼠标右键--->属性--->高级--->环境变量--->系统变量--->找到名称为Path的--->编辑--->在变量值最后面加上pthreadVC2.dll的完整路径(本人的为C:\WINDOWS\system32\pthreadVC2.dll)。

三、测试pthreads扩展

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

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

附上一个Thinkphp3.2.2简单例子

<?php
namespace Home\Controller;
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

windows下安装php真正的多线程扩展pthreads教程的更多相关文章

  1. Windows下安装Redis及php的redis拓展教程

    一.安装前必读 Windows 64位操作系统 Redis 安装包(版本3.0.5,截止2017-05-29最新redis版本为3.2.9) 注意事项: 1.在window下如果你还需安装php的re ...

  2. windows下安装php5.5的redis扩展

    windows下开发用的xampp集成的环境,想装个php-redis扩展,扩展的github地址:  https://github.com/nicolasff/phpredis php_redis. ...

  3. windows下安装redis 以及phpredis的扩展 (windows redis php&php7)

    一.工具准备 1. redis for windows 下载 https://github.com/MSOpenTech/redis 2. PHP扩展下载 http://pecl.php.net/pa ...

  4. windows下安装oracle客户端和php扩展

    先来抱怨下 ,按这玩楞费了我大半天的时间,一路的坑! 我的电脑是win7 64位的 第一步  打开php.ini  把 extension=php_oci8_12c.dll extension=php ...

  5. Windows下PHP多线程扩展pthreads的安装

    pthreads扩展安装步骤 1.查看phpinfo() 获取PHP版本号及位数(x86表示32位,x64表示64位).编译器版本.PHP配置文件加载所在位置等.如下图所示: 2.pthreads扩展 ...

  6. windows下安装redis和php的redis扩展

    1.redis简介 redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset(s ...

  7. Windows下安装PHP扩展及资源下载地址(memcached为例)

    官方下载的php安装包ext目录里以经包含了常用的php扩展,但某些情况下并不能满足我们项目需求,比如memcache扩展就不在官方的php安装包里.这时就需要我们自己去下载安装. 本文列出php官方 ...

  8. windows下安装redis以及redis扩展,设置redis为windows自启服务

    windows下安装reids windows下redis下载地址:https://github.com/MSOpenTech/redis/releases. 启动redis服务:在redis目录下启 ...

  9. 在windows环境下安装redis和phpredis的扩展

    在windows环境下安装redis和phpredis的扩展 1.首先配置php: 需要在windows的集成环境中找到php的扩展文件夹,ext,然后在网上寻找自己的php对应的.dll文件 比如说 ...

随机推荐

  1. 自己动手用Javascript写一个无刷新分页控件

    .NET技术交流群:337901356 ,欢迎您的加入! 对 于一个用户体验好的网站来说,无刷新技术是很重要的,无刷新,顾名思义,就是局部刷新数据,有用过Asp.net Web Form技术开发网页的 ...

  2. php 中json_decode()和json_encode()的使用方法

    1.json_decode() json_decode (PHP 5 >= 5.2.0, PECL json >= 1.2.0) json_decode — 对 JSON 格式的字符串进行 ...

  3. php生成验证码图片

    0:效果图 1:index.php用来生成验证码图片 <?php session_start(); header ('Content-Type: image/png'); $image=imag ...

  4. jquery 可拖动进度条

    实现这个效果怎么弄呢? <!DOCTYPE html> <html> <head lang="en"> <meta charset=&qu ...

  5. NUnit使用详解(二)

    转载:http://hi.baidu.com/grayworm/item/39aa11c5d9375d56bdef6990 五:常用断言 在NUnit中,断言是单元测试的核心.NUnit提供了一组丰富 ...

  6. hadoop_集群安装_2

    由于上一篇文章http://www.cnblogs.com/inuyasha1027/p/hadoop_cluster_install_1.html 截图太多,占用了太多的地方,所以将VMTools ...

  7. Web开发必备资源汇总[转]

    导读:原文来自< Best “must know” open sources to build the new Web>,译文由酷壳网陈皓整理编译< 开源中最好的Web开发的资源 & ...

  8. 学习笔记_过滤器详细(过滤器JavaWeb三大组件之一)

    过滤器详细 1 过滤器的生命周期 我们已经学习过Servlet的生命周期,那么Filter的生命周期也就没有什么难度了! (l)  init(FilterConfig):在服务器启动时会创建Filte ...

  9. iOS8 【xcode6中添加pch全局引用文件】

    前沿:xcode6中去掉了pch,为了一些琐碎的头文件引用,加快了 编译速度! xcode6之前的版本建项目就自动添加了是这样的: xcode6后的版本要自己手动的添加步骤如下: 1)  2) 3) ...

  10. 在ARM Linux 使用 Valgrind

    Linux valgrind 移植到ARM-Linux  一.Cross-Compile/交叉编译 (1)下载及解压Valgrind-3.11 (2)修改confirure 将armv7*)修改为ar ...