十年網(wǎng)站開發(fā)經(jīng)驗(yàn) + 多家企業(yè)客戶 + 靠譜的建站團(tuán)隊(duì)
量身定制 + 運(yùn)營維護(hù)+專業(yè)推廣+無憂售后,網(wǎng)站問題一站解決
這篇文章主要介紹了thinkphp5.1怎么實(shí)現(xiàn)多線程爬蟲,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
創(chuàng)新互聯(lián)是專業(yè)的龍陵網(wǎng)站建設(shè)公司,龍陵接單;提供成都網(wǎng)站設(shè)計(jì)、網(wǎng)站制作,網(wǎng)頁設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行龍陵網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來合作!
創(chuàng)建一個(gè)cli命令
php think make:command Thread thread
測(cè)試能否成功執(zhí)行
php think thread
文檔地址:guzzle文檔地址(https://guzzle-cn.readthedocs.io/zh_CN/latest/quickstart.html)
setName('thread'); // 設(shè)置參數(shù) } protected function execute(Input $input, Output $output) { $client = new Client(); $requests = function ($total) use ($client) { foreach (range(1, $total) as $r) { $uri = 'https://apinew.juejin.im/content_api/v1/short_msg/detail'; yield function () use ($client, $uri) { return $client->postAsync($uri, [ 'verify' => false, 'json' => [ 'msg_id' => '6845185452727599118' ] ]); }; } }; $pool = new Pool($client, $requests($this->totalPageCount), [ 'concurrency' => $this->threads, // 請(qǐng)求成功 'fulfilled' => function ($response, $index) use ($output) { $res = $response->getBody()->getContents(); $output->writeln($res); $output->writeln("正在執(zhí)行第{$index}個(gè)·····"); if ($this->checkThreadIsEnd() == true) { $output->writeln("------------請(qǐng)求結(jié)束---------"); return false; } }, // 請(qǐng)求失敗 'rejected' => function ($reason, $index) use ($output) { $output->writeln("執(zhí)行失敗,{$reason}"); }, ]); $promise = $pool->promise(); $promise->wait(); } /** * 檢測(cè)任務(wù)是否結(jié)束 * @return bool */ private function checkThreadIsEnd() { if (self::$counter < $this->totalPageCount) { self::$counter++; return false; } else { return true; } } }
php think thread
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“thinkphp5.1怎么實(shí)現(xiàn)多線程爬蟲”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來學(xué)習(xí)!