<!-- [if gte mso 9]><xml>
<w:WordDocument>
<w:View>Normal</w:View>
<w:Zoom>0</w:Zoom>
<w:PunctuationKerning/>
<w:DrawingGridVerticalSpacing>7.8 磅</w:DrawingGridVerticalSpacing>
<w:DisplayHorizontalDrawingGridEvery>0</w:DisplayHorizontalDrawingGridEvery>
<w:DisplayVerticalDrawingGridEvery>2</w:DisplayVerticalDrawingGridEvery>
<w:ValidateAgainstSchemas/>
<w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid>
<w:IgnoreMixedContent>false</w:IgnoreMixedContent>
<w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText>
<w:Compatibility>
<w:SpaceForUL/>
<w:BalanceSingleByteDoubleByteWidth/>
<w:DoNotLeaveBackslashAlone/>
<w:ULTrailSpace/>
<w:DoNotExpandShiftReturn/>
<w:AdjustLineHeightInTable/>
<w:BreakWrappedTables/>
<w:SnapToGridInCell/>
<w:WrapTextWithPunct/>
<w:UseAsianBreakRules/>
<w:DontGrowAutofit/>
<w:UseFELayout/>
</w:Compatibility>
<w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel>
</w:WordDocument>
</xml><![endif]--><!-- [if gte mso 9]><xml>
<w:LatentStyles DefLockedState="false" LatentStyleCount="156">
</w:LatentStyles>
</xml><![endif]--><!-- [if gte mso 10]>
<style><!--mce:0-->
最近在公司的项目中,有一个功能,将 EXECL 中的数据批量导入到数据库里。 本地并发只能通过语言自己的特性在程序本身实现多任务效果,一般来说现在的语言会通过多线程或多进程的方式来实现。由于 PHP 不支持多线程,目前只能采用多进程方式,让操作系统来帮助实现本地并发。
public function httpRequest($params){
//{"header":{"operation":"api.getScoreData"}, "params":{"cname":"ddddd","cpwd":"ddddd"}}
$data = array("tb" =>$params);
// return json data
$data_string = json_encode($data);
$tmp = $params[0]['savename'];
$pid = $params['project_id'];
// request url host http://localhost/webinsights/adminLink.php/Link/test/$tmp/$pid
//$url = U("Link/test/$tmp","$pid");
$ch = curl_init("http://localhost/webinsights/adminLink.php/Link/writeDataBase/$tmp/$pid");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
curl_exec($ch);
curl_close($ch);
}
public function writeDataBase(){
//测试 php 异步 执行
//sleep(10);
$execlname = $_REQUEST['_URL_'][2];
$project_id = $_REQUEST['_URL_'][3];
$name = substr($execlname ,0,13 );
$arr = F($name);
$model = D('ItemsDetail');
$data = array();
ignore_user_abort(true); // run script in background
set_time_limit(0);
foreach ( $arr as $k=>$v )
{
$data['linkname'] = $v['B'];
$data['linkurl'] = $v['C'];
$data['linktext'] = $v['F'];
$data['loginurl'] = $v['E'];
$data['pr'] = $v['D'];
$data['project_id'] = $project_id;
//info信息
$data['ItemsInfo'] = array(
"project_id" => $project_id,
"lxname" => $v['A'],
"lxqq" => $v['K'],
"lxemail" => $v['M'],
"lxtel" => $v['L'],
"fkname" => $v['J'],
"fkaccount" => $v['I'],
"fkprice" => $v['H'],
);
$model->batAdd($data);
sleep(1);
}
F($name,NULL);
exit;
}
public function httpRequest($params){
//{"header":{"operation":"api.getScoreData"}, "params":{"cname":"ddddd","cpwd":"ddddd"}}
$data = array("tb" =>$params);
// return json data
$data_string = json_encode($data);
$tmp = $params[0]['savename'];
$pid = $params['project_id'];
// request url host http://localhost/webinsights/adminLink.php/Link/test/$tmp/$pid
//$url = U("Link/test/$tmp","$pid");
$ch = curl_init("http://localhost/webinsights/adminLink.php/Link/writeDataBase/$tmp/$pid");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
curl_exec($ch);
curl_close($ch);
}
public function writeDataBase(){
//测试 php 异步 执行
//sleep(10);
$execlname = $_REQUEST['_URL_'][2];
$project_id = $_REQUEST['_URL_'][3];
$name = substr($execlname ,0,13 );
$arr = F($name);
$model = D('ItemsDetail');
$data = array();
ignore_user_abort(true); // run script in background
set_time_limit(0);
foreach ( $arr as $k=>$v )
{
$data['linkname'] = $v['B'];
$data['linkurl'] = $v['C'];
$data['linktext'] = $v['F'];
$data['loginurl'] = $v['E'];
$data['pr'] = $v['D'];
$data['project_id'] = $project_id;
//info信息
$data['ItemsInfo'] = array(
"project_id" => $project_id,
"lxname" => $v['A'],
"lxqq" => $v['K'],
"lxemail" => $v['M'],
"lxtel" => $v['L'],
"fkname" => $v['J'],
"fkaccount" => $v['I'],
"fkprice" => $v['H'],
);
$model->batAdd($data);
sleep(1);
}
F($name,NULL);
exit;
}
public function httpRequest($params){
//{"header":{"operation":"api.getScoreData"}, "params":{"cname":"ddddd","cpwd":"ddddd"}}
$data = array("tb" =>$params);
// return json data
$data_string = json_encode($data);
$tmp = $params[0]['savename'];
$pid = $params['project_id'];
// request url host http://localhost/webinsights/adminLink.php/Link/test/$tmp/$pid
//$url = U("Link/test/$tmp","$pid");
$ch = curl_init("http://localhost/webinsights/adminLink.php/Link/writeDataBase/$tmp/$pid");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
curl_exec($ch);
curl_close($ch);
}
public function writeDataBase(){
//测试 php 异步 执行
//sleep(10);
$execlname = $_REQUEST['_URL_'][2];
$project_id = $_REQUEST['_URL_'][3];
$name = substr($execlname ,0,13 );
$arr = F($name);
$model = D('ItemsDetail');
$data = array();
ignore_user_abort(true); // run script in background
set_time_limit(0);
foreach ( $arr as $k=>$v )
{
$data['linkname'] = $v['B'];
$data['linkurl'] = $v['C'];
$data['linktext'] = $v['F'];
$data['loginurl'] = $v['E'];
$data['pr'] = $v['D'];
$data['project_id'] = $project_id;
//info信息
$data['ItemsInfo'] = array(
"project_id" => $project_id,
"lxname" => $v['A'],
"lxqq" => $v['K'],
"lxemail" => $v['M'],
"lxtel" => $v['L'],
"fkname" => $v['J'],
"fkaccount" => $v['I'],
"fkprice" => $v['H'],
);
$model->batAdd($data);
sleep(1);
}
F($name,NULL);
exit;
}
<!-- [if gte mso 9]><xml>
<w:WordDocument>
<w:View>Normal</w:View>
<w:Zoom>0</w:Zoom>
<w:PunctuationKerning/>
<w:DrawingGridVerticalSpacing>7.8 磅</w:DrawingGridVerticalSpacing>
<w:DisplayHorizontalDrawingGridEvery>0</w:DisplayHorizontalDrawingGridEvery>
<w:DisplayVerticalDrawingGridEvery>2</w:DisplayVerticalDrawingGridEvery>
<w:ValidateAgainstSchemas/>
<w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid>
<w:IgnoreMixedContent>false</w:IgnoreMixedContent>
<w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText>
<w:Compatibility>
<w:SpaceForUL/>
<w:BalanceSingleByteDoubleByteWidth/>
<w:DoNotLeaveBackslashAlone/>
<w:ULTrailSpace/>
<w:DoNotExpandShiftReturn/>
<w:AdjustLineHeightInTable/>
<w:BreakWrappedTables/>
<w:SnapToGridInCell/>
<w:WrapTextWithPunct/>
<w:UseAsianBreakRules/>
<w:DontGrowAutofit/>
<w:UseFELayout/>
</w:Compatibility>
<w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel>
</w:WordDocument>
</xml><![endif]--><!-- [if gte mso 9]><xml>
<w:LatentStyles DefLockedState="false" LatentStyleCount="156">
</w:LatentStyles>
</xml><![endif]--><!-- [if gte mso 10]>
<style><!--mce:1-->
上面是关键代码部分,下面解释一下 ignore_user_abort(true) set_time_limit(0) sleep(1) 这三个 PHP 函数用途
第一个是让 PHP 在后台自己执行代码 其目的是当用户关闭浏览器后后代码仍然在执行,也就说用户只要把 EXECL 文件传入到服务器后,任务就完成了,不需要开着浏览器 直到数据导入完毕。
第二个函数 控制 PHP 的执行时间;
第三个函数 是为了分散服务器压力 这样在用户导入大量的数据过程中 用户可以继续查看部分导入成功的数据信息
关于 httpRequest ()函数的目的是 使用 PHP 开启一个进程
End