日日操夜夜添-日日操影院-日日草夜夜操-日日干干-精品一区二区三区波多野结衣-精品一区二区三区高清免费不卡

公告:魔扣目錄網(wǎng)為廣大站長提供免費收錄網(wǎng)站服務,提交前請做好本站友鏈:【 網(wǎng)站目錄:http://www.ylptlb.cn 】, 免友鏈快審服務(50元/站),

點擊這里在線咨詢客服
新站提交
  • 網(wǎng)站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會員:747

創(chuàng)建一個cli命令

php think make:command Thread thread

登錄后復制

測試能否成功執(zhí)行

php think thread

登錄后復制登錄后復制

安裝Guzzle類庫

文檔地址:guzzle文檔地址(https://guzzle-cn.readthedocs.io/zh_CN/latest/quickstart.html)

實現(xiàn)代碼

<?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
{

    /**
     * 請求的總次數(shù)
     * @var int
     */
    protected $totalPageCount = 50;
    /**
     * 當前請求的次數(shù)
     * @var int
     */
    protected static $counter = 1;
    /**
     * 線程的數(shù)量
     * @var int
     */
    protected $threads = 20;

    protected function configure()
    {
        // 指令配置
        $this->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,
            // 請求成功
            'fulfilled' => function ($response, $index) use ($output) {
                $res = $response->getBody()->getContents();
                $output->writeln($res);
                $output->writeln("正在執(zhí)行第{$index}個·····");
                if ($this->checkThreadIsEnd() == true) {
                    $output->writeln("------------請求結(jié)束---------");
                    return false;
                }
            },
            // 請求失敗
            'rejected' => function ($reason, $index) use ($output) {
                $output->writeln("執(zhí)行失敗,{$reason}");
            },
        ]);
        $promise = $pool->promise();
        $promise->wait();
    }

    /**
     * 檢測任務是否結(jié)束
     * @return bool
     */
    private function checkThreadIsEnd()
    {
        if (self::$counter < $this->totalPageCount) {
            self::$counter++;
            return false;
        } else {
            return true;
        }
    }


}

登錄后復制

執(zhí)行命令

php think thread

登錄后復制登錄后復制

以上就是thinkphp5.1怎么實現(xiàn)多線程爬蟲的詳細內(nèi)容,更多請關(guān)注www.xfxf.net其它相關(guān)文章!

分享到:
標簽:thinkphp
用戶無頭像

網(wǎng)友整理

注冊時間:

網(wǎng)站:5 個   小程序:0 個  文章:12 篇

  • 51998

    網(wǎng)站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會員

趕快注冊賬號,推廣您的網(wǎng)站吧!
最新入駐小程序

數(shù)獨大挑戰(zhàn)2018-06-03

數(shù)獨一種數(shù)學游戲,玩家需要根據(jù)9

答題星2018-06-03

您可以通過答題星輕松地創(chuàng)建試卷

全階人生考試2018-06-03

各種考試題,題庫,初中,高中,大學四六

運動步數(shù)有氧達人2018-06-03

記錄運動步數(shù),積累氧氣值。還可偷

每日養(yǎng)生app2018-06-03

每日養(yǎng)生,天天健康

體育訓練成績評定2018-06-03

通用課目體育訓練成績評定