創建一個cli命令
php think make:command Thread thread
登錄后復制
測試能否成功執行
php think thread
登錄后復制登錄后復制
安裝Guzzle類庫
文檔地址:guzzle文檔地址(https://guzzle-cn.readthedocs.io/zh_CN/latest/quickstart.html)
實現代碼
<?php
/**
* Created by.
* User: Jim
* Date: 2020/9/29
* Time: 14:31
*/
namespace app\command;
use GuzzleHttp\Client;
use GuzzleHttp\Pool;
use think\console\Command;
use think\console\Input;
use think\console\Output;
/**
* Guzzle
* Class Thread
* @package app\command
* 文檔地址 https://guzzle-cn.readthedocs.io/zh_CN/latest/quickstart.html
*/
class Thread extends Command
{
/**
* 請求的總次數
* @var int
*/
protected $totalPageCount = 50;
/**
* 當前請求的次數
* @var int
*/
protected static $counter = 1;
/**
* 線程的數量
* @var int
*/
protected $threads = 20;
protected function configure()
{
// 指令配置
$this->setName('thread');
// 設置參數
}
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,
// 請求成功
'fulfilled' => function ($response, $index) use ($output) {
$res = $response->getBody()->getContents();
$output->writeln($res);
$output->writeln("正在執行第{$index}個·····");
if ($this->checkThreadIsEnd() == true) {
$output->writeln("------------請求結束---------");
return false;
}
},
// 請求失敗
'rejected' => function ($reason, $index) use ($output) {
$output->writeln("執行失敗,{$reason}");
},
]);
$promise = $pool->promise();
$promise->wait();
}
/**
* 檢測任務是否結束
* @return bool
*/
private function checkThreadIsEnd()
{
if (self::$counter < $this->totalPageCount) {
self::$counter++;
return false;
} else {
return true;
}
}
}
登錄后復制
執行命令
php think thread
登錄后復制登錄后復制
以上就是thinkphp5.1怎么實現多線程爬蟲的詳細內容,更多請關注www.xfxf.net其它相關文章!






