php的多线程使用
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位的版本。
1、修改php.ini文件 添加extension=php_pthreads.dll
2、将php_pthreads.dll放到C:\wamp\bin\php\php5.5.12\ext下面
<?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
php的多线程使用的更多相关文章
- Python中的多进程与多线程(一)
一.背景 最近在Azkaban的测试工作中,需要在测试环境下模拟线上的调度场景进行稳定性测试.故而重操python旧业,通过python编写脚本来构造类似线上的调度场景.在脚本编写过程中,碰到这样一个 ...
- 多线程爬坑之路-Thread和Runable源码解析之基本方法的运用实例
前面的文章:多线程爬坑之路-学习多线程需要来了解哪些东西?(concurrent并发包的数据结构和线程池,Locks锁,Atomic原子类) 多线程爬坑之路-Thread和Runable源码解析 前面 ...
- 多线程爬坑之路-学习多线程需要来了解哪些东西?(concurrent并发包的数据结构和线程池,Locks锁,Atomic原子类)
前言:刚学习了一段机器学习,最近需要重构一个java项目,又赶过来看java.大多是线程代码,没办法,那时候总觉得多线程是个很难的部分很少用到,所以一直没下决定去啃,那些年留下的坑,总是得自己跳进去填 ...
- Java多线程
一:进程与线程 概述:几乎任何的操作系统都支持运行多个任务,通常一个任务就是一个程序,而一个程序就是一个进程.当一个进程运行时,内部可能包括多个顺序执行流,每个顺序执行流就是一个线程. 进程:进程 ...
- .NET基础拾遗(5)多线程开发基础
Index : (1)类型语法.内存管理和垃圾回收基础 (2)面向对象的实现和异常的处理基础 (3)字符串.集合与流 (4)委托.事件.反射与特性 (5)多线程开发基础 (6)ADO.NET与数据库开 ...
- Java多线程基础——对象及变量并发访问
在开发多线程程序时,如果每个多线程处理的事情都不一样,每个线程都互不相关,这样开发的过程就非常轻松.但是很多时候,多线程程序是需要同时访问同一个对象,或者变量的.这样,一个对象同时被多个线程访问,会出 ...
- C#多线程之线程池篇3
在上一篇C#多线程之线程池篇2中,我们主要学习了线程池和并行度以及如何实现取消选项的相关知识.在这一篇中,我们主要学习如何使用等待句柄和超时.使用计时器和使用BackgroundWorker组件的相关 ...
- C#多线程之线程池篇2
在上一篇C#多线程之线程池篇1中,我们主要学习了如何在线程池中调用委托以及如何在线程池中执行异步操作,在这篇中,我们将学习线程池和并行度.实现取消选项的相关知识. 三.线程池和并行度 在这一小节中,我 ...
- C#多线程之线程池篇1
在C#多线程之线程池篇中,我们将学习多线程访问共享资源的一些通用的技术,我们将学习到以下知识点: 在线程池中调用委托 在线程池中执行异步操作 线程池和并行度 实现取消选项 使用等待句柄和超时 使用计时 ...
- C#多线程之线程同步篇3
在上一篇C#多线程之线程同步篇2中,我们主要学习了AutoResetEvent构造.ManualResetEventSlim构造和CountdownEvent构造,在这一篇中,我们将学习Barrier ...
随机推荐
- mysql数据库的连接
public TJb checkjbByschool(long id)throws ClassNotFoundException,SQLException { Class.forName(" ...
- 子元素用margin-top 为什么反而作用在父元素上?对使用margin-top 的元素本身不起作用?
在这个说明中,“collapsing margins”(折叠margin)的意思是:2个或以上盒模型之间(关系可以是相邻或嵌套)相邻的margin属性(这之间不能有非空内容.padding区域.bor ...
- sql每五秒插入一条数据 一次插入N条数据
1建立数据表 create table projectManage ( ID int identity primary key not null, projectName )not null, man ...
- ASP.NET MVC 4.0 学习2-留言板實現
新增專案實現留言板功能,瞭解MVC的運行機制 1,新增專案 2,添加數據庫文件message.mdf Ctrl+W,L 打開資料庫連接,添加存放留言的Atricle表 添加字段,後點擊&quo ...
- Kafka与Logstash的数据采集
Kafka与Logstash的数据采集 基于Logstash跑通Kafka还是需要注意很多东西,最重要的就是理解Kafka的原理. Logstash工作原理 由于Kafka采用解耦的设计思想,并非原始 ...
- Primo Ramdisk配置教程
首先感谢xiaohu在太平洋电脑网上发表的“将内存当硬盘用!Primo Ramdisk图文教程”,本文主要是将其图文整理了一下,以方便以后使用. 原文地址:http://fashion.pconlin ...
- Service、Alarm与BroadcastReceiver的使用方法
1:定义一个服务类,在服务类中使用AlarmManager 来管理服务的运行 public class WtacService extends Service{ private AlarmManage ...
- C# 伪造 referer 提交数据
private string SendRequest(string account, string cardNumber, string cardPass) { string targetUrl = ...
- 使用 asp.net Web API 2的坑
使用工具: Googl 浏览器+PostMan 插件 写了个 控制器 添加了个Action,结果呢?GET 方式请求没问题. POST一直,在服务器端获取不了参数...找了官方的文档 .各种雨里雾 ...
- man/ls/clock/date/echo笔记
login: 用户名:用户ID 认证机制:Authentication授权:Authorization审计:Audition (日志) prompt,命令提示符:命令:magic numb ...