1 <?php 2 3 use \Swoole\Coroutine; 4 use function \Swoole\Coroutine\run; 5 use \Swoole\Coroutine\Channel; 6 7 // 参考链接:https://wiki.swoole.com/#/coroutine/channel?id=%e5%ae%9e%e7%8e%b0%e5%8e%9f%e7%90%86 8 9 run(function () use ($restult) { 10 $channel = new Channel(50); 11 12 Coroutine::create(function () use ($channel, $restult) { 13 foreach ($restult as $key => $value) { 14 $channel->push(['data' => $value, 'index' => $key]); 15 } 16 }); 17 18 Coroutine::create(function () use ($channel) { 19 while (1) { 20 $data = $channel->pop(2.0); 21 if ($data) { 22 try { 23 // do something 24 } catch (\Exception $e) { 25 } 26 } else { 27 assert($channel->errCode === \SWOOLE_CHANNEL_TIMEOUT); 28 break; 29 } 30 } 31 }); 32 });
标签:function,rshutdown,shutdown,data,Event,channel From: https://www.cnblogs.com/qk1314/p/17224226.html