PHP 5.3 以上版本,使用pthreads PHP扩展,可以使PHP真正地支持多线程。多线程在处理重复性的循环任务,能够大大缩短程序执行时间。

在liunx下的安装

准备工作:

1.下载Threading for PHP安装包https://github.com/krakjoe/pthreads

2.php安装包

php安装时一定要加上--enable-maintainer-zts参数    这个是安全线程

yum install php-devel php-pear httpd-devel 
wget http://www.php.net/distributions/php-5.5.8.tar.gz 
tar zxvf php-5.5.8.tar.gz
cd php-5.5.8.tar.gz
./configure --prefix=/usr/local/php --with-config-file-path=/etc --enable-maintainer-zts
make && make install
( make -j3 && make -j3 install) -> Faster building
cp php.ini-development /etc/php.ini
pecl install pthreads
提示错误
error: pthreads requires ZTS, please re-compile PHP with ZTS enabled
不对啊 我加了安全线程参数了 怎么还报错。。。
好像刚才用yum安装php-devel的时候安装了php
于是
yum remove php
重新编译在安装
pecl install pthreads
提示错误 ERROR: `phpize' failed
cd /usr/local/php/bin/
查看是否有phpize
发现有phpize,说明不是php-devel的问题
是安装扩展的方式出了问题,所以php安装是没有问题的
下载pthreads扩展包
unzip pthreads-master.zip
cd pthreads
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config
make && make install
提示Installing shared extensions:     /usr/local/php/lib/php/extensions/no-debug-zts-20121212/
说明安装成功
修改php.ini文件,添加extension=pthreads.so
echo "extension=pthreads.so" >> /etc/php.ini
php -v
提示Warning: PHP Startup: Unable to load dynamic library '/usr/lib64/php/modules/pthreads.so' - /usr/lib64/php/modules/pthreads.so: cannot open shared object file: No such file or directory in Unknown on line 0
尼玛都没一件顺心的安装
错误 我擦
查看一下 /usr/local/php/include/php/main/php_config.php 里面是否有#define ZTS 1
里面有这个啊 我擦 这是什么问题呢 继续百度 重新安装一次试试看 cd php-5.5.8.tar.gz ./configure --prefix=/usr/local/php --with-config-file-path=/etc --enable-maintainer-zts
make -j3 && make -j3 install
cp php.ini-development /etc/php.ini

进入pthreads解压目录
cd ~/soft/pthreads-master
运行phpize脚本
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config
make && make install
提示安装成功
echo "extension=pthreads.so" >> /etc/php.ini
php -v

PHP Warning:  PHP Startup: Unable to load dynamic library '/usr/lib64/php/modules/pthreads.so' - /usr/lib64/php/modules/pthreads.so: undefined symbol: core_globals_id in Unknown on line 0

尼玛还是同样的错误   不知道了

windows下多线程的使用

本人php版本是5.4.17的所以下载php_pthreads-0.1.0-5.4-ts-vc9-x86.zip文件包,其中0.1.0表示为当前pthreads版本号,5.4为php版本号,ts就是之前判断php对应的ts、nts版,vs9代表是Visual Studio 2008 compiler编译器编译的,最后的x86代表的是32位的版本。

 
将下载好的php_pthreads-0.1.0-5.4-ts-vc9-x86.zip文件包解压找到
pthreadVC2.dll和php_pthreads.dll文件.

1、修改php.ini文件 添加extension=php_pthreads.dll
2、将php_pthreads.dll放到C:\wamp\bin\php\php5.5.12\ext下面

3、将pthreadVC2.dll放到C:\wamp\bin\php\php5.5.12下面
4、将pthreadVC2.dll放到C:\Windows\System32下面
5、将pthreadVC2.dll的绝对路径放到path环境变量里面
3、重启Apache服务器
<?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();
}
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, );
curl_setopt($curl, CURLOPT_TIMEOUT, );
curl_setopt($curl, CURLOPT_USERAGENT, $userAgent);
$result = curl_exec($curl);
curl_close($curl);
return $result;
} for ($i=; $i < ; $i++)
{
$urls_array[] = array("name" => "baidu", "url" => "http://www.baidu.com/s?wd=".mt_rand(,));
} $t = microtime(true);
$result = model_thread_result_get($urls_array);
$e = microtime(true);
echo "多线程:".($e-$t)."\n"; $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)."\n";
?>

多线程:5.1022920608521 For循环:20.272159099579

多线程花的时间比单线程花的时间少的多。
windows下多线程php测试成功!!!

php的多线程使用的更多相关文章

  1. Python中的多进程与多线程(一)

    一.背景 最近在Azkaban的测试工作中,需要在测试环境下模拟线上的调度场景进行稳定性测试.故而重操python旧业,通过python编写脚本来构造类似线上的调度场景.在脚本编写过程中,碰到这样一个 ...

  2. 多线程爬坑之路-Thread和Runable源码解析之基本方法的运用实例

    前面的文章:多线程爬坑之路-学习多线程需要来了解哪些东西?(concurrent并发包的数据结构和线程池,Locks锁,Atomic原子类) 多线程爬坑之路-Thread和Runable源码解析 前面 ...

  3. 多线程爬坑之路-学习多线程需要来了解哪些东西?(concurrent并发包的数据结构和线程池,Locks锁,Atomic原子类)

    前言:刚学习了一段机器学习,最近需要重构一个java项目,又赶过来看java.大多是线程代码,没办法,那时候总觉得多线程是个很难的部分很少用到,所以一直没下决定去啃,那些年留下的坑,总是得自己跳进去填 ...

  4. Java多线程

    一:进程与线程 概述:几乎任何的操作系统都支持运行多个任务,通常一个任务就是一个程序,而一个程序就是一个进程.当一个进程运行时,内部可能包括多个顺序执行流,每个顺序执行流就是一个线程.   进程:进程 ...

  5. .NET基础拾遗(5)多线程开发基础

    Index : (1)类型语法.内存管理和垃圾回收基础 (2)面向对象的实现和异常的处理基础 (3)字符串.集合与流 (4)委托.事件.反射与特性 (5)多线程开发基础 (6)ADO.NET与数据库开 ...

  6. Java多线程基础——对象及变量并发访问

    在开发多线程程序时,如果每个多线程处理的事情都不一样,每个线程都互不相关,这样开发的过程就非常轻松.但是很多时候,多线程程序是需要同时访问同一个对象,或者变量的.这样,一个对象同时被多个线程访问,会出 ...

  7. C#多线程之线程池篇3

    在上一篇C#多线程之线程池篇2中,我们主要学习了线程池和并行度以及如何实现取消选项的相关知识.在这一篇中,我们主要学习如何使用等待句柄和超时.使用计时器和使用BackgroundWorker组件的相关 ...

  8. C#多线程之线程池篇2

    在上一篇C#多线程之线程池篇1中,我们主要学习了如何在线程池中调用委托以及如何在线程池中执行异步操作,在这篇中,我们将学习线程池和并行度.实现取消选项的相关知识. 三.线程池和并行度 在这一小节中,我 ...

  9. C#多线程之线程池篇1

    在C#多线程之线程池篇中,我们将学习多线程访问共享资源的一些通用的技术,我们将学习到以下知识点: 在线程池中调用委托 在线程池中执行异步操作 线程池和并行度 实现取消选项 使用等待句柄和超时 使用计时 ...

  10. C#多线程之线程同步篇3

    在上一篇C#多线程之线程同步篇2中,我们主要学习了AutoResetEvent构造.ManualResetEventSlim构造和CountdownEvent构造,在这一篇中,我们将学习Barrier ...

随机推荐

  1. web.xml中classpath:和classpath*: 有什么区别?

    web.xml中classpath:和classpath*:     IccBoY applicationContext.xml 配置文件的存放位置 web.xml中classpath:和classp ...

  2. [C++程序设计]指向数组元素的指针

    如果先使p指向数组a的首元素(即p=a),则: (1) p++(或p+=1).使p指向下一元素,即a[1]. 如果用*p,得到下一个元素a[1]的值. (2) *p++.由于++和*同优先级,结合方向 ...

  3. django中使用json.dumps处理数据时,在前台遇到字符转义的问题

    django后台代码: import json ctx['dormitory_list'] = json.dumps([{", "is_checked": 1}, {&q ...

  4. 常见的SQL字符串函数

    1.LEN:计算字符串的长度(字符的个数) select len('哈哈hello') 返回长度为7 2.datalength();计算字符串所占用的字节数,不属于字符串函数 select DATAL ...

  5. apache主目录,配置文件目录结构说明

    apache服务安装成功后,主要的目录结构如下: |-- bin 程序命令目录[apache执行文件的目录如apachectl,htpassed] |-- build |-- cgi-bin 预设给一 ...

  6. Debain 7.2安装配置

    一 下载安装Debian 7.2 安装debian CD1,在最后一步,使用网络安装基本界面. 二 修改源 cd /etc/apt mv sources.list sources.list.bak g ...

  7. android fragment 跳到另一个fragment

    一共有4个fragment,分别是contact(联系人),friends(朋友),search(查找),more(更多).使用的都是同一个布局,每个fragment中都有四个内部按钮,可以切换到其他 ...

  8. sqlserver 创建索引

    语法:CREATE [索引类型] INDEX 索引名称ON 表名(列名)WITH FILLFACTOR = 填充因子值0~100GO /*实例*/  CREATE NONCLUSTERED INDEX ...

  9. Unique Binary Search Trees II 解答

    Question Given n, generate all structurally unique BST's (binary search trees) that store values 1.. ...

  10. HDU5137 How Many Maos Does the Guanxi Worth(枚举+dijkstra)

    How Many Maos Does the Guanxi Worth Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 512000/5 ...