php pthreads 多线程扩展的使用:一个较为稳定例子。
今天研究了worker stackable的配合方法,写了两种形式,虽然能工作,但是都会出现内存不听增长的问题;
于是把第一个方法的代码邮件给了作者,到现在他没有回复我。
我最后放弃两者配合的方式,直接使用worker,发现到现在执行了582000个工作,内存没有增长,速度也基本稳定,代码如下:
<?php
ini_set('zend.enable_gc', true); class ExampleWorker extends Worker {
public $stack_count = 0;
public function __construct() {}
public function run(){
sleep(1);//waiting for data
$count = 0;
$data_flag = true;
$stime = microtime(true);
while(1){
if($data = $this->shift()){
$data_flag = true;
$this->doSomeWork();
if((++$count)%1000==0){
printf("Work Mermory used %.3fMB RAM, time: %3f===> %d \n",
memory_get_peak_usage(true)/1048576, (microtime(true) - $stime), $count);
$stime = microtime(true);
}
}else{
usleep(100000);
}
}
exit;
}
private function doSomeWork(){
$str = 'sdalsadkfkasd;lfksa;ldfkas;lkf;lsadkf;as';
$len = strlen($str); $s = substr($str,rand(0,$len));
$len = floor(strlen($s)/2); for($i=0;$i<$len;$i++){
$tmp = $s[$i];
$idx = intval($len-$i-1); $s[$i] = $s[$idx];
$s[$idx] = $tmp;
}
}
} $worker= new ExampleWorker(sprintf("Worker [%d]", 0));
$worker->start(); $stime = microtime(true);
$count = 3000000;//99999999;
$total = $count*5; $ct = 0;
while($count--){
//for ($target = 0; $target < 5; $target++)
$worker[]='data_'.(++$ct);
usleep(rand(200,1800));
}
//sleep(10);
$runtime = (microtime(true)-$stime);
printf("---------------------------------------------------------\n");
printf("Used time is %f \n", $runtime);
printf("Mermory used %.3fMB RAM, added %d, left %d\n", memory_get_peak_usage(true)/1048576, $total, count($worker));
printf("---------------------------------------------------------\n"); //var_dump($works);
虚拟上的运行速度:
Work Mermory used 0.250MB RAM, time: 2.141709===> 607000
Work Mermory used 0.250MB RAM, time: 1.721918===> 608000
Work Mermory used 0.250MB RAM, time: 1.858363===> 609000
Work Mermory used 0.250MB RAM, time: 1.734542===> 610000
Work Mermory used 0.250MB RAM, time: 1.819794===> 611000
Work Mermory used 0.250MB RAM, time: 1.847132===> 612000
Work Mermory used 0.250MB RAM, time: 1.740353===> 613000
Work Mermory used 0.250MB RAM, time: 1.628364===> 614000
Work Mermory used 0.250MB RAM, time: 1.731518===> 615000
Work Mermory used 0.250MB RAM, time: 1.730583===> 616000
Work Mermory used 0.250MB RAM, time: 1.825315===> 617000
Work Mermory used 0.250MB RAM, time: 1.762334===> 618000
Work Mermory used 0.250MB RAM, time: 1.842860===> 619000
Work Mermory used 0.250MB RAM, time: 1.732677===> 620000
php pthreads 多线程扩展的使用:一个较为稳定例子。的更多相关文章
- PHP安装pthreads多线程扩展教程[windows篇]
from:http://blog.csdn.net/aoyoo111/article/details/19020161 一.判断PHP是ts还是nts版 通过phpinfo(); 查看其中的 Thre ...
- windows下xampp安装PHP的pthreads多线程扩展
我的运行环境: 系统:windows10 ,64位 PHP:5.6.8 TS,VC11 ,32位 Apache: 2.0 我安装的是xampp集成环境 pthreads的windows扩展文件下载地址 ...
- Windows环境下PHP安装pthreads多线程扩展
一.判断PHP是ts还是nts版 通过phpinfo(); 查看其中的 Thread Safety 项,这个项目就是查看是否是线程安全,如果是:enabled,一般来说应该是ts版,否则是nts版. ...
- Windows下PHP多线程扩展pthreads的安装
pthreads扩展安装步骤 1.查看phpinfo() 获取PHP版本号及位数(x86表示32位,x64表示64位).编译器版本.PHP配置文件加载所在位置等.如下图所示: 2.pthreads扩展 ...
- Unicode其实是Latin1的扩展。只有一个低字节的Uncode字符其实就是Latin1字符——附各种字符编码表及转换表
一.概念 1,ASCII ASCII(American Standard Code for Information Interchange),中文名称为美国信息交换标准代码.是 ...
- Linux是一个基于POSIX和Unix的多用户、多任务、支持多线程和多CPU的性能稳定的操作系统,可免费使用并自由传播。
Linux是一个基于POSIX和Unix的多用户.多任务.支持多线程和多CPU的性能稳定的操作系统,可免费使用并自由传播. Linux是众多操作系统之一 , 目前流行的服务器和 PC 端操作系统有 L ...
- .NET Core的文件系统[5]:扩展文件系统构建一个简易版“云盘”
FileProvider构建了一个抽象文件系统,作为它的两个具体实现,PhysicalFileProvider和EmbeddedFileProvider则分别为我们构建了一个物理文件系统和程序集内嵌文 ...
- [转]自己写PHP扩展之创建一个类
原文:http://www.imsiren.com/archives/572 比如我们要创建一个类..PHP代码如下 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 ...
- linq to sql用partial扩展属性,创建一个部分类(用于多表连接)
1.在窗体中创建dataGridView显示表: using System; using System.Collections.Generic; using System.ComponentModel ...
随机推荐
- Spring读书笔记-----Spring的Bean之Bean的基本概念
从前面我们知道Spring其实就是一个大型的工厂,而Spring容器中的Bean就是该工厂的产品.对于Spring容器能够生产那些产品,则取决于配置文件中配置. 对于我们而言,我们使用Spring框架 ...
- adb_常用命令
1. adb push 电脑中的文件(包含路径) Android中的绝对路径 2. adb pull Android中的绝对路径文件 电脑中的绝对路径 3. adb install ??.ap ...
- 36个炫丽的html5 canvas展示
36个炫丽的html5 canvas展示http://html6game.com/2013/08/03/36-behind-the-html5-canvas-show.shtml 16个最好的CSS3 ...
- numpy库的常用知识
为什么有numpy这个库呢?准安装的Python中用列表(list)保存一组值,可以用来当作数组使用,不过由于列表的元素可以是任何对象,因此列表中所保存的是对象的指针.这样为了保存一个简单的[1,2, ...
- 2 CSS
2 CSS CSS基础 html 在一个网页中负责的事情是一个页面的结构css(层叠样式表) 在一个网页中主要负责了页面的数据样式. 编写css代码的方式: 第一种: 在style标签中编写c ...
- JQuery $()后面的括号里的内容什么时候加引号,什么时候不加
一.如果是已经声明存在的变量或者对象,就不用加引号. 比如var name=document.getElementById("name"); $(name)或者$(this). 二 ...
- C#_数据库连接串的配置
数据库的连接串 数据库连接字符串的拼写规则的决定条件: 连接的数据库的类型:SQL Server,Oracle,MySQL,Acess,MogoDB,Visual FoxPro(dBASE),Exce ...
- 转!!Java JTable 根据表格内容 自动调整表格列宽
//根据表格内容 自动调整列宽http://blog.sina.com.cn/s/blog_5e54d6140100s1d3.html
- mac10.12的Cocopods安装使用
Cocopods的安装 CocoaPods应该是iOS最常用最有名的类库管理当我们开发iOS应用时,会经常使用到很多第三方开源类库,比如AFNetWorking等等,可能某个类库又用到其他的库,手动一 ...
- nginx日志分析手机使用频次
__author__ = 'similarface' from collections import defaultdict import glob ip = r"?P<ip>[ ...