Home  >  Article  >  Backend Development  >  Implementing the Timer function in PHP uses pcntl and libevent

Implementing the Timer function in PHP uses pcntl and libevent

WBOY
WBOYOriginal
2016-07-25 09:13:291017browse
  1. function newChild($func_name) {
  2. echo "enter newChildn";
  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: ^_^Good PHP development communication group: 256271784 , Verification: cxy, if you are interested, you can join in and discuss)
  15. function on_timer() {
  16. echo "timer calledn";
  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 timern";
  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 exitn";
  34. }
Copy code

This article comes from the "Qingyuan Education" blog, please indicate here when reprinting, thank you! Welcome to log in to the official website of Qingyuan Education to view more video tutorials.



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn