当前位置:Gxlcms > PHP教程 > PHP中实现Timer功能使用pcntl和libevent

PHP中实现Timer功能使用pcntl和libevent

时间:2021-07-01 10:21:17 帮助过:40人阅读

  1. function newChild($func_name) {
  2. echo "enter newChild\n";
  3. $args = func_get_args();
  4. unset($args[0]);
  5. $pid = pcntl_fork();
  6. if ($pid == 0) {
  7. function_exists($func_name) and exit(call_user_func_array($func_name, $args)) or exit(-1);
  8. } else if($pid == -1) {
  9. echo "Couldn't create child process";
  10. } else {
  11. return $pid;
  12. }
  13. }
  14. (PS:^_^不错的php开发交流群:256271784,验证:cxy,有兴趣的话可以加入进来一起讨论)
  15. function on_timer() {
  16. echo "timer called\n";
  17. }
  18. /**
  19. * @param $func string, function name
  20. * @param $timeouts int, microtimes for time delay
  21. */
  22. function timer($func, $timeouts){
  23. echo "enter timer\n";
  24. $base = event_base_new();
  25. $event = event_new();
  26. event_set($event, 0, EV_TIMEOUT, $func);
  27. event_base_set($event, $base);
  28. event_add($event, $timeouts);
  29. event_base_loop($base);
  30. }
  31. $pid = newChild("timer", "on_timer", 5000000);
  32. if ($pid > 0) {
  33. echo "master process exit\n";
  34. }

本文出自 “清源教育” 博客,转载请注明此处,谢谢!欢迎登录清源教育官网,查看更多视频教程。

人气教程排行