创建一个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 appcommand;
use GuzzleHttpClient;
use GuzzleHttpPool;
use thinkconsoleCommand;
use thinkconsoleInput;
use thinkconsoleOutput;
/**
- Guzzle
- Class Thread
- @package appcommand
- 文档地址 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怎么实现多线程爬虫的详细内容,更多请关注php中文网其它相关文章!


BrianSab9 天前
发表在:南通速强批量添加水印专家 v1.83Эта статья для ознак...
Ronaldgag11 天前
发表在:11日17日,星期一,在这里每天60秒读懂世界!Free PHP Blockchain ...
NelsonBOT12 天前
发表在:11日16日,星期日,在这里每天60秒读懂世界!Free non-criminal in...
Darrenjhjhjhcunny14 天前
发表在:11日14日,星期五,在这里每天60秒读懂世界!Атака черной материи...
parifoot-rdc-7925 天前
发表在:laravel 找不到页面Votre guide <a href=...
Anya142Sa1 个月前
发表在:ASUS华硕A8N-SLI Deluxe主板BIOS 10110Hello friends! I c...
91资源网站长-冰晨1 个月前
发表在:广告合作123
FrankFAT1 个月前
发表在:10日14日,星期二,在这里每天60秒读懂世界!Big cocks of blacks ...
RichardGlymn1 个月前
发表在:Java webservice多个参数怎么调用https://t.me/win_1_c...
Thomasstolo1 个月前
发表在:Java webservice多个参数怎么调用https://t.me/s/Casin...