在日常處理使用php處理業務邏輯的時候總會遇到過快執行與慢執行同時都要執行,例如表單上傳的時候要發送郵件給用戶,同時用戶也是非常多,不可能提交很少的數據提交時間需要10-20秒這個時候客戶就會覺得好垃圾啊,什麼鳥程序。這個時候他來了他來了他腳踏祥雲swoole他來了。
下面代碼運行是這樣的例如https://www.xxx.com/index.php?_acid=3&s=/api/play/qingyouji.html這個是我要快速輸出給前端的東西。
下面這部分東西是我要異步的東西。把我要異步的都放置守護進程來完成。
$urls=[
'https://www.xxx.com/index.php?_acid=3&s=/api/index/index.html',
'https://www.xxx.com/index.php?_acid=3&s=/api/index/tehui.html',
'https://www.xxx.com/index.php?_acid=3&s=/api/play/dangjituijian.html',
'https://www.xxx.com/index.php?_acid=3&s=/api/play/qingyouji.html'
];
最後瀏覽器上運行 http:xxx.aizxy.cn:9501就可以了,剩下的API接口要怎麼寫我就不廢話了。
<?php
$http = new swoole_http_server("xxx.aizxy.cn", 9501);
$http->set([
'worker_num' =>3, //工作進程數
'daemonize' => true, //是否後臺運行
]);
$http->on('request','Run');
function Run($request, $response)
{
$url=$request->server['request_uri'];
if($url!='/favicon.ico'){
$urls=[
'https://www.xxx.com/index.php?_acid=3&s=/api/index/index.html',
'https://www.xxx.com/index.php?_acid=3&s=/api/index/tehui.html',
'https://www.xxx.com/index.php?_acid=3&s=/api/play/dangjituijian.html',
'https://www.xxx.com/index.php?_acid=3&s=/api/play/qingyouji.html'
];
handel($urls);
}
$data='https://www.xxx.com/index.php?_acid=3&s=/api/play/qingyouji.html';
$data=file_get_contents($data);
$response->end($data);
}
//處理進程
function handel($urls)
{
$worker_num = 5;
$process_pool = [];
$process= null;
$pid = posix_getpid();
$customMsgKey = 1;
$mod = 2 | swoole_process::IPC_NOWAIT;//這裏設置消息隊列爲非阻塞模式
for($i=0;$i<$worker_num; $i++) {
$process=new swoole_process('sub_process');
$process->useQueue($customMsgKey, $mod);
$process->start();
$pid = $process->pid;
$process_pool[$pid] = $process;
}
//由於所有進程是共享使用一個消息隊列,所以只需向一個子進程發送消息即可
$process = current($process_pool);
foreach ($urls as $url) {
$process->push($url);
}
}
//管道
function sub_process(swoole_process $worker)
{
sleep(1); //防止父進程還未往消息隊列中加入內容直接退出
while($url = $worker->pop()){
if ($url === false) {
break;
}
$sub_pid = $worker->pid;
$content=file_get_contents($url);//異步請求不返回
//file_put_contents(__DIR__.'/a.txt',"[$sub_pid] url : $url.$content".PHP_EOL,FILE_APPEND);
sleep(1);//這裏的sleep模擬任務耗時,否則可能1個worker就把所有信息全接受了
}
$worker->exit(0);
}
$http->start();
有些小夥伴如果進程被佔用了netstat -anp | grep 9501
然後再殺死進程kill -9 10908
最後再次運行即可。