Use Pcntl and libevent to implement the timer function in PHP

Source: Internet
Author: User
  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: ^_^ Good PHP Development Exchange Group: 256271784, verify: Cxy, if interested can join in to discuss together)
  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. }
Copy Code
This article from "Qingyuan Education" blog, reproduced please specify here, thank you! Welcome to the official website of Qingyuan education to see more video tutorials.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.