18947 stop GET index.php at:1504513052 cost:4
18948 stop GET index.php at:1504513053 cost:5
18949 stop GET index.php at:1504513054 cost:6
All request handle stop at 1504513054 cost:6
如果依次处理请求,总耗时为1+2+3+4+5+6=21s。每个请求创建一个进程的处理方式,总耗时是最耗时的请求操作6s。
多进程最好在方法、函数或者文件中单独使用,这样逻辑更加清晰,也便于分析和维护。
附录
邮件操作类:
MailWork.php
<?php
/**
* Created by PhpStorm.
* User: zhezhao
* Date: 2017/9/4
* Time: 上午10:05
*/
class MailWork
{
private $error;
private $name;
public function __construct($name)
{
$this->name = $name;
}
public function getLastError(){
return $this->error;
}
public function checkEnv($argc)
{
if (substr(php_sapi_name(), 0, 3) !== 'cli') {
$this->error ="This Programe can only be run in CLI mode";
return false;
}
if($argc!=2){
$this->error = 'wrong params';
return false;
}
return true;
}
public function getFileName($argv){
$filename = "task".$argv[1];
if(!file_exists($filename)){
$this->error = 'file does not exits';
return false;
}else{
return $filename;
}
}
public function sendMail($mailto)
{
sleep(2);
echo $this->name."#mailto:".$mailto.PHP_EOL;
}
public function text_mail($argc,$argv){
if(!$this->checkEnv($argc)){
return false;
}
$filename = $this->getFileName($argv);
if(!$filename){
return false;
}
$fp = fopen($filename,'r');
$counter = 0;
while(!feof($fp)){
$line = fgets($fp);
$mailto = substr($line,0,strlen($line)-1);
if(preg_match('/^\w+@\w+\.\w+$/',$mailto)){
$this->sendMail($mailto);
$counter++;
}
}
return $counter;
}
public function redis_mail($mailto){
if(preg_match('/^\w+@\w+\.\w+$/',$mailto)){
$this->sendMail($mailto);
}
}
}
更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP进程与线程操作技巧总结》、《PHP网络编程技巧总结》、《PHP基本语法入门教程》、《PHP数组(Array)操作技巧大全》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家PHP程序设计有所帮助。
