test.php 729 B

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