<?php
var $StartTime = 0;
var $StopTime = 0;
function get_microtime(){
list($usec, $sec) = explode(' ', microtime());
return ((float)$usec + (float)$sec);
}
function start(){
$this->StartTime = $this->get_microtime();
}
function stop(){
$this->StopTime = $this->get_microtime();
}
function spent(){
return round(($this->StopTime - $this->StartTime) * 1000, 1);
}
function idtocdn(){
$list = Article::get_instance()->select([
'where'=>"content_time>='2017-5-1' and content_time<='2017-5-2'",
'field'=>'id'
]);
$redis_key = "idtocdn";
if(is_array($list)){
foreach($list as $k=>$v){
$id = $v["id"];
\Io\Redis::i('redis')->Sadd($redis_key,$id);
}
}
}
function multithread(){
$type = $this->params()->get('type');
//echo "type=>".$type;
if(!$type){
echo "no type";exit;
}
if (!function_exists("pcntl_fork")) {
die("pcntl extention is must !");
}
//总进程的数量
$totals = 50;
// 执行的脚本数量
$cmdArr = array();
// 执行的脚本数量的数组
for ($i = 0; $i < $totals; $i++) {
$cmdArr[] = array('pid'=>$i, 'total'=>$totals);
}
pcntl_signal(SIGCHLD, SIG_IGN); //如果父进程不关心子进程什么时候结束,子进程结束后,内核会回收。
foreach ($cmdArr as $cmd) {
$pid = pcntl_fork(); //创建子进程
//父进程和子进程都会执行下面代码
if ($pid == -1) {
//错误处理:创建子进程失败时返回-1.
die('could not fork');
} else if ($pid) {
//父进程会得到子进程号,所以这里是父进程执行的逻辑
//如果不需要阻塞进程,而又想得到子进程的退出状态,则可以注释掉pcntl_wait($status)语句,或写成:
pcntl_wait($status,WNOHANG); //等待子进程中断,防止子进程成为僵尸进程。
} else {
//子进程得到的$pid为0, 所以这里是子进程执行的逻辑。
if($type==1){
exec("/app/php5/bin/php /app/index.php cron/createhtml");
exit(0);
}elseif($type==2){
exec("/app/php5/bin/php /app/index.php cron/pushcdn/cdn_upload_type/2");
exit(0);
}
}
}
}
public function createhtml(){
while(1){
$redis_key = "idtocdn";
$id = \Io\Redis::i('redis')->Spop($redis_key);
if($id>0){
$this->start();
$list = Article::get_instance()->select([
'where'=>"id='$id'",
'field'=>'id,content'
]);
ob_clean();
foreach($list as $v){
$date=date("Y-m-d H:i:s");
file_put_contents("/tmp/cdn_log.log",$v['id'].".html date=>".$date."\r\n",FILE_APPEND);
$html_file_root = \Config::get("html")['path'];
$content_arr = @json_decode($v['content'],1);
$count = @count($content_arr);
if(is_array($content_arr) && $count>1){ //是数组,json过的文章
for ($i =1;$i<=$count;$i++){//有分页
if($i==1){//第一页
$_GET['id'] = $v['id'];
$_GET['page'] = $i;
$ctr = new \Index\Controller\Index();
$this->_file_count++;
$html = $v['id'].'.html';
$html_file = $html_file_root."/detail/" . $html;
$ctr->detail();
$data = ob_get_contents();
$ret = \Io\File::output($html_file,$data,true);
ob_clean();
$redis_key_out = "htmlcdn";
\Io\Redis::i('redis')->Sadd($redis_key_out,$id);
}else{//其他页
$_GET['id'] = $v['id'];
$_GET['page'] = $i;
$ctr = new \Index\Controller\Index();
$this->_file_count++;
$html = $v['id'].'_'.$i.'.html';
$html_file = $html_file_root."/detail/" . $html;
$ctr->detail();
$data = ob_get_contents();
$ret = \Io\File::output($html_file,$data,true);
ob_clean();
$redis_key_out = "htmlcdn";
\Io\Redis::i('redis')->Sadd($redis_key_out,$id.'_'.$i);
}
}
}else{ //文章没有分页
$_GET['id'] = $v['id'];
$_GET['page'] = 1;
$ctr = new \Index\Controller\Index();
$this->_file_count++;
$html = $v['id'].'.html';
$html_file = $html_file_root."/detail/" . $html;
$ctr->detail();
$data = ob_get_contents();
$ret = \Io\File::output($html_file,$data,true);
ob_clean();
$redis_key_out = "htmlcdn";
\Io\Redis::i('redis')->Sadd($redis_key_out,$id);
}
}
$this->stop();
$str = "页面执行时间: ".$this->spent()." 毫秒";
file_put_contents("/tmp/cdn_log.log", "spent=>".$str."\r\n",FILE_APPEND);
}
if($id==""){
break;
}
}
}
//推荐到cdn
function pushcdn(){
while(1){
$redis_key = "htmlcdn";
$id = \Io\Redis::i('redis')->Spop($redis_key);
file_put_contents("/tmp/pushcdn",$id."\r\n",FILE_APPEND);
$html_file_root = \Config::get("html")['path'];
if ($this->_cdn_upload_type == 2 ){
//立刻上传至cdn
//ob_clean();
$html_file = $html_file_root."/detail/".$id.".html";
if($this->_cdn_bucket->upload("detail/".$id.".html",$html_file)){
echo "true";
}else{
echo "cdn_upload false";
}
}
ob_clean();
if($id==""){
break;
}
}
}
http://blog.csdn.net/u013372487/article/details/51994898
shell实现 http://www.111cn.net/phper/31/90896.htm
https://www.oschina.net/code/snippet_779765_44045
1.首先看下 phpize命令 所在的目录 (ps:我的目录/usr/bin/phpize)
如果没有找到的话 执行安装
yum install php_devel
安装完毕后。会生成phpize命令
2.去php.net下载相应版本的php源文件
咱们以php-5.4.45 为例吧,解压后,进入相应的模块下
cd php-5.4.45/ext/pcntl
#先执行phpize /usr/bin/phpize
./configure --with-php-config=/usr/bin/php-config
#安装 make && make install
3.编译完毕后会生成了一个 pcntl.so的文件。在php的model目录里
编辑/etc/php.ini,加入
extension=pcntl.so
4.重启apache
service httpd restart
5.测试是否安装成功
<?php echo pcntl_fork(); ?>
输出:23165(不一定是这个数,但一定是个数字)
注意:
使用:/usr/bin/phpize 可能出现下面错误提示
Can't find PHP headers in /usr/include/php
The php-devel package is required for use of this command
解决方法:
yum install php-devel
http://blog.csdn.net/e421083458/article/details/22186475
- <?php
- header('content-type:text/html;charset=utf-8' );
- // 必须加载扩展
- if (!function_exists("pcntl_fork")) {
- die("pcntl extention is must !");
- }
- //总进程的数量
- $totals = 3;
- // 执行的脚本数量
- $cmdArr = array();
- // 执行的脚本数量的数组
- for ($i = 0; $i < $totals; $i++) {
- $cmdArr[] = array("path" => __DIR__ . "/run.php", 'pid' =>$i ,'total' =>$totals);
- }
- /*
- 展开:$cmdArr
- Array
- (
- [0] => Array
- (
- [path] => /var/www/html/company/pcntl/run.php
- [pid] => 0
- [total] => 3
- )
- [1] => Array
- (
- [path] => /var/www/html/company/pcntl/run.php
- [pid] => 1
- [total] => 3
- )
- [2] => Array
- (
- [path] => /var/www/html/company/pcntl/run.php
- [pid] => 2
- [total] => 3
- )
- )
- */
- pcntl_signal(SIGCHLD, SIG_IGN); //如果父进程不关心子进程什么时候结束,子进程结束后,内核会回收。
- foreach ($cmdArr as $cmd) {
- $pid = pcntl_fork(); //创建子进程
- //父进程和子进程都会执行下面代码
- if ($pid == -1) {
- //错误处理:创建子进程失败时返回-1.
- die('could not fork');
- } else if ($pid) {
- //父进程会得到子进程号,所以这里是父进程执行的逻辑
- //如果不需要阻塞进程,而又想得到子进程的退出状态,则可以注释掉pcntl_wait($status)语句,或写成:
- pcntl_wait($status,WNOHANG); //等待子进程中断,防止子进程成为僵尸进程。
- } else {
- //子进程得到的$pid为0, 所以这里是子进程执行的逻辑。
- $path = $cmd["path"];
- $pid = $cmd['pid'] ;
- $total = $cmd['total'] ;
- echo exec("/usr/bin/php {$path} {$pid} {$total}")."\n";
- exit(0) ;
- }
- }
- ?>