swoole的协程是单进程单线程的,是不能利用多核的,想使用多核需要通过添加work数来实现。这里和go本质区别就是,worker内的全局函数是进程内共享的,全局共享需要通过共享内存等其他方式实现;
hyperf封装的协程基本有四种方式,第一种就是go或co关键字,通过管道channel通讯来并行处理;第二种是通过waitgroup;前两种发现和go几乎一样,第三种通过Parallel;第四种使用Parallel的全局函数,其实都是对前面两种的封装
上代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
<?php declare(strict_types=1); /** * This file is part of Hyperf. * * @link https://www.hyperf.io * @document https://hyperf.wiki * @contact group@hyperf.io * @license https://github.com/hyperf/hyperf/blob/master/LICENSE */ namespace App\Controller; use Hyperf\Di\Annotation\Inject; use Hyperf\HttpServer\Annotation\AutoController; use Hyperf\HttpServer\Contract\RequestInterface; use Hyperf\Utils\Parallel; use Hyperf\Utils\WaitGroup; use Swoole\Coroutine\Channel; /** * @AutoController */ class IndexController extends AbstractController { /** * @Inject * @var \Hyperf\Guzzle\ClientFactory */ private $clientFactory; public function index() { $user = $this->request->input('user', 'Hyperf'); $method = $this->request->getMethod(); return [ 'method' => $method, 'message' => "Hello {$user}.", ]; } public function sleep(RequestInterface $request) { $seconds = $request->input('seconds', 1); sleep((int) $seconds); return $seconds; } public function test() { // $channel = new Channel(); // co(function () use ($channel) { // $client = $this->clientFactory->create(); // $res = $client->get('127.0.0.1:9501/index/sleep?seconds=3'); // $channel->push(123); // }); // co(function () use ($channel) { // $client = $this->clientFactory->create(); // $res = $client->get('127.0.0.1:9501/index/sleep?seconds=2'); // $rs = $res->getBody()->getContents(); // $channel->push($rs); // }); // $result = []; // $result[] = $channel->pop(); // $result[] = $channel->pop(); //第二种方式 // $wg = new WaitGroup(); // $result = []; // $wg->add(2); // $client = $this->clientFactory->create(); // co(function () use ($wg, &$result, $client) { // $res = $client->get('127.0.0.1:9501/index/sleep?seconds=3'); // $rs = $res->getBody()->getContents(); // $result[] = $rs; // $wg->done(); // }); // co(function () use ($wg, &$result, $client) { // $res = $client->get('127.0.0.1:9501/index/sleep?seconds=2'); // $rs = $res->getBody()->getContents(); // $result[] = $rs; // $wg->done(); // }); // $wg->wait(); //第三种方式 // $parall = new Parallel(); // $parall->add(function () { // $client = $this->clientFactory->create(); // $res = $client->get('127.0.0.1:9501/index/sleep?seconds=3'); // return $res->getBody()->getContents(); // }); // $parall->add(function () { // $client = $this->clientFactory->create(); // $res = $client->get('127.0.0.1:9501/index/sleep?seconds=2'); // return $res->getBody()->getContents(); // }); // return $parall->wait(); //第四种 return parallel([ 'a' => function () { $client = $this->clientFactory->create(); $res = $client->get('127.0.0.1:9501/index/sleep?seconds=3'); return $res->getBody()->getContents(); }, 'b' => function () { $client = $this->clientFactory->create(); $res = $client->get('127.0.0.1:9501/index/sleep?seconds=2'); return $res->getBody()->getContents(); }, ]); } } |
程序本天成,妙手偶得之!我们只是代码的搬运工!
转载请注明:http://www.521php.com/archives/2087/
2021年12月08日 上午 9:41 自媒体运营 | 引用 | #1
不错,必须顶一下!