test.php 688 B

12345678910111213141516171819202122232425262728293031323334
  1. <?php
  2. function BatchExecMethodByCo($channel,$funcs)
  3. {
  4. foreach ($funcs as $key => $func) {
  5. go(function()use($channel,$func,$key){
  6. $res = $func();
  7. $channel->push([$key=>$res]);
  8. });
  9. }
  10. }
  11. function test($value='')
  12. {
  13. \Co::sleep(1);
  14. return "test\n";
  15. }
  16. function test2($value='')
  17. {
  18. \Co::sleep(1);
  19. return "test2 ".rand(1,10)."\n";
  20. }
  21. go(function(){
  22. $c = 2;
  23. $channel = new \Swoole\Coroutine\Channel(2);
  24. $task = ["test","test2","test"];
  25. BatchExecMethodByCo($channel,$task);
  26. $list = [];
  27. $num = count($task);
  28. for ($i=0;$i<$num;$i++)
  29. {
  30. $list[$i] = $channel->pop();
  31. }
  32. var_dump($list);
  33. });