您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
php并发拉取数据再优化(解决有时候body为空问题及新增支持从指定数量开始向目标数量拉取)
发布时间:2022-06-15 22:08:47编辑:雪饮阅读()
<?php
namespace app\home\command;
use think\facade\App;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\Db;
use think\Env;
use function Swoole\Coroutine\run;
use function Swoole\Coroutine\go;
use Swoole\Coroutine\Channel;
class SeedProductManagementByStart extends Command
{
protected function configure()
{
$this->setName('SeedProductManagementByStart')->setDescription('种子产品管理(指定开始数据id)');
}
public static function executeGroup2($group,$start){
//执行分组
$len=count($group);
$chan = new Channel($len);
$host='cha.191.cn';
for($i=1;$i<=$len;$i++){
go(function () use ($chan,$i,$host,$group,$start) {
$key="k_".$group[$i-1];
$val="";
if($group[$i-1]>=$start){
$cli = new \Swoole\Coroutine\Http\Client($host, 443,true);
$cli->set(['timeout' => 10]);
$cli->setHeaders([
'Host' => $host,
"User-Agent" => 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36',
'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
'Accept-Encoding' => 'gzip, deflate, br',
]);
// $token="8194277bbb57b042107e812c0a1dfe06b48859dd1654823766465";
// $url='/api3/product/detailInfo?des=3cecQI03vqoM%2FVP2LyGwpN%2FXlI0WrUC9eSmmhvkHXMpX%2FSk4vNI1jJNW1lfxGA&product_id='.$group[$i-1].'&device_id=HKL59YKE&version=2.4.0&sys=android&token='.$token.'&deviceId=HKL59YKE&channleId=';
$token2="ae87059ed9237d8593d7e1e2db46d259807398591655185910552";
$url2='/api3/product/detailInfo?des=dea0GEpTo%2FXN%2FaJ86II9Xm36yl4AyPEsz9OjHYzzjIPMUbVtkjsYwe6JI9gcdQ&product_id='.$group[$i-1].'&device_id=HKL59YKE&version=2.4.0&sys=android&token='.$token2.'&deviceId=HKL59YKE&channleId=';
$cli->get($url2);
$statusCode=$cli->getStatusCode();
$body=$cli->body;
if(!$body){
//对方并发能力承受不住时候可能会出现该问题
$body="empty";
var_dump("body为空,状态值:".$statusCode.",errCode:".$cli->errCode.",错误详情:".socket_strerror($cli->errCode));
exit();
}
$val=$body;
$chan->push([$key=>$val]);
$cli->close();
}
else{
var_dump($group[$i-1]."跳过");
$chan->push([$key=>$val]);
}
});
}
for ($i = 1; $i <=$len; $i++)
{
$current_result=$chan->pop();
$keys=array_keys($current_result);
$json_to_arr=json_decode($current_result[$keys[0]],true);
// file_put_contents("meta_data_".$group[$i-1],var_export($json_to_arr,true));
//var_dump("000000000000000");
if(isset($json_to_arr["data"]) && isset($json_to_arr["data"]["info"]) && isset($json_to_arr['data']['info']['id'])){
//var_dump("1111111111");
$info=$json_to_arr['data']['info'];
$dir_path=ROOT_PATH."/mission_meta";
if(!is_dir($dir_path)){
mkdir($dir_path);
}
$log_file_name="meta_data_".$group[$i-1];
file_put_contents($dir_path."/".$log_file_name,var_export($json_to_arr,true));
$insert = [
//测试id
'test_id' => $info['id'],
//审定编号
'approval_no' => $info['registry'],
//作物种类
'crop_species' => $info['name'],
//品种名称
'name' => $info['product_name'],
//企业ID
'seed_enterprise_id' => $info['company_id'],
//审定单位
'approved_country' => isset($info['auditing_body']) ? $info['auditing_body']:'',
//审定年份
'year' => isset($info['register_year']) ? $info['register_year']:'',
//品种来源
'source' => isset($info['source']) ? $info['source']:'',
//是否转基因
'is_transgene' => isset($info['gmo']) ? $info['gmo']:'否',
// 特征特性
'features' => isset($info['features']) ? $info['features']:'',
//产量表现
'performance' => isset($info['yield']) ? $info['yield']:'',
//适宜区域
'suitable_area' => isset($info['suitable_range']) ? $info['suitable_range']:'',
//实验情况
'experimental_situation' => isset($info['experiments']) ? $info['experiments']:'',
//创建时间
'createtime' => time(),
//更新时间
'updatetime' => time(),
// 权重
'weigh' => rand(1, 9999),
'status' => 'normal',
];
//$count=Db::name('fa_seed_product')->where('test_id',$info['id'])->count();
$count=0;
if($count==0){
// Db::name('fa_seed_product')->insert($insert);
}
else{
$dir_path=ROOT_PATH."/mission_exist";
if(!is_dir($dir_path)){
mkdir($dir_path);
}
$log_file_name="meta_data_".$group[$i-1];
file_put_contents($dir_path."/".$log_file_name,var_export($json_to_arr,true));
}
}
else{
//var_dump("2222222");
$dir_path=ROOT_PATH."/mission_failed";
if(!is_dir($dir_path)){
mkdir($dir_path);
}
$log_file_name="meta_data_".$group[$i-1];
file_put_contents($dir_path."/".$log_file_name,var_export($json_to_arr,true));
}
//var_dump("当前数据:");
// var_dump($json_to_arr);
var_dump("------".$group[$i-1]."执行完成");
if($i==$len){
//var_dump("分组".($group_index+1)."执行完成");
return true;
}
}
}
protected function execute(Input $input, Output $output)
{
$start_time=time();
//分组
$total_rows=149827;
//分组大小,分组大小越大越接近 "Too many open files"错误的触发
/*
*
*
*
在这个文件vim /etc/security/limits.conf末尾加上"* - nofile 8192"
最前的 * 表示所有用户,可根据需要设置某一用户,例如
user1 soft nofile 8192
user1 hard nofile 8192
注意”nofile”项有两个可能的限制措施。就是项下的hard和soft。 要使修改过得最大打开文件数生效,必须对这两种限制进行设定。 如果使用”-“字符设定, 则hard和soft设定会同时被设定。
*
*
* */
$start=1;
$groups_size=6000;
$group_num=ceil($total_rows/$groups_size);
$groups=[];
for($i=1;$i<=$group_num;$i++){
$last_id=0;
$last_group_index=count($groups)-1;
if($last_group_index>-1){
$last_group_data_index=count($groups[$last_group_index])-1;
$last_id=$groups[$last_group_index][$last_group_data_index];
}
$really_group_size=$groups_size;
if($i==$group_num){
$really_group_size=$total_rows-$groups_size*($i-1);
}
$cureent_group=[];
for($x=1;$x<=$really_group_size;$x++){
$push_id=$last_id+$x;
$cureent_group[]=$push_id;
}
$groups[]=$cureent_group;
}
//分组完成
\Swoole\Coroutine\run(function() use($groups,$start,$start_time){
file_put_contents("getStatusCode.txt","");
$len=count($groups);
for($i=1;$i<=$len;$i++){
$group=$groups[$i-1];
var_dump("分组".$i."开始执行");
$res=Self::executeGroup2($group,$start);
var_dump("分组".$i."执行完成");
}
$end_time=time();
$use_time=$end_time-$start_time;
$use_time_i=floor($use_time/60);
$use_time_s=$use_time-($use_time_i*60);
var_dump("耗时:".$use_time_i."分".$use_time_s."秒");
});
}
}
关键字词:php,并发,拉取,body
上一篇:pixel4a刷机流程