thinkphp5.1ÔõôʵÏÖ¶àÏß³ÌÅÀ³æ
½¨ÉèÒ»¸ö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ÔõôʵÏÖ¶àÏß³ÌÅÀ³æµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡
ÃâÔð˵Ã÷£ºÒÔÉÏչʾÄÚÈÝȪԴÓÚÏàÖúýÌå¡¢ÆóÒµ»ú¹¹¡¢ÍøÓÑÌṩ»òÍøÂçÍøÂçÕûÀí£¬°æȨÕùÒéÓë±¾Õ¾Î޹أ¬ÎÄÕÂÉæ¼°¿´·¨Óë¿´·¨²»´ú±í尊龙凯时人生就是搏ÂËÓÍ»úÍø¹Ù·½Ì¬¶È£¬Çë¶ÁÕß½ö×ö²Î¿¼¡£±¾ÎĽӴýתÔØ£¬×ªÔØÇë˵Ã÷À´ÓÉ¡£ÈôÄúÒÔΪ±¾ÎÄÇÖÕ¼ÁËÄúµÄ°æȨÐÅÏ¢£¬»òÄú·¢Ã÷¸ÃÄÚÈÝÓÐÈκÎÉæ¼°ÓÐÎ¥¹«µÂ¡¢Ã°·¸Ö´·¨µÈÎ¥·¨ÐÅÏ¢£¬ÇëÄúÁ¬Ã¦ÁªÏµ尊龙凯时人生就是搏ʵʱÐÞÕý»òɾ³ý¡£