Use pcntl and libevent to implement the Timer function in PHP
- Function newChild ($ func_name ){
- Echo "enter newChild \ n ";
- $ Args = func_get_args ();
- Unset ($ args [0]);
- $ Pid = pcntl_fork ();
- If ($ pid = 0 ){
- Function_exists ($ func_name) and exit (call_user_func_array ($ func_name, $ args) or exit (-1 );
- } Else if ($ pid =-1 ){
- Echo "Couldn't create child process ";
- } Else {
- Return $ pid;
- }
- }
- (PS: ^ _ ^ A good php Development Exchange Group: 256271784, verification: cxy. if you are interested, join in for discussion)
- Function on_timer (){
- Echo "timer called \ n ";
- }
- /**
- * @ Param $ func string, function name
- * @ Param $ timeouts int, microtimes for time delay
- */
- Function timer ($ func, $ timeouts ){
- Echo "enter timer \ n ";
- $ Base = event_base_new ();
- $ Event = event_new ();
- Event_set ($ event, 0, EV_TIMEOUT, $ func );
- Event_base_set ($ event, $ base );
- Event_add ($ event, $ timeouts );
- Event_base_loop ($ base );
- }
- $ Pid = newChild ("timer", "on_timer", 5000000 );
- If ($ pid> 0 ){
- Echo "master process exit \ n ";
- }
This article is from the "Qingyuan education" blog. For more information, see here. thank you! Log on to Qingyuan education official website to view more video tutorials.
|