Recently I was working on a project that required the use of asynchronous PHP. When I was looking through the PHP source code, I found three unused modules, sysvsem, sysvshm, and sysvmsg. After some research, I benefited a lot.
There is such a family of functions in PHP, which are packages of the V IPC function family of UNIX.
They are rarely used by people, but they are very powerful. Using them wisely can get you twice the result with half the effort.
They include:
Semaphores
##Shared Memory
Inter-Process Messaging (IPC)
Based on these, it is entirely possible for us to package PHP into a message-driven system. But, first, we need to cover a few important basics: 1. ftokint ftok ( string pathname, string proj ) //ftok将一个路径名pathname和一个项目名(必须为一个字符), 转化成一个整形的用来使用系统V IPC的key
function getStatus($arg){ print_r connection_status(); debug_print_backtrace(); } reigster_tick_function("getStatus", true); declare(ticks=1){ for($i =1; $i<999; $i++){ echo "hello"; } } unregister_tick_function("getStatus");
function getStatus($arg){ print_r connection_status(); debug_print_backtrace(); } reigster_tick_function("getStatus", true); declare(ticks=1){ for($i =1; $i<999; $i++){ echo "hello"; getStatus(true); } } unregister_tick_function("getStatus");
$mesg_key = ftok(__FILE__, 'm'); $mesg_id = msg_get_queue($mesg_key, 0666); function fetchMessage($mesg_id){ if(!is_resource($mesg_id)){ print_r("Mesg Queue is not Ready"); } if(msg_receive($mesg_id, 0, $mesg_type, 1024, $mesg, false, MSG_IPC_NOWAIT)){ print_r("Process got a new incoming MSG: $mesg "); } } register_tick_function("fetchMessage", $mesg_id); declare(ticks=2){ $i = 0; while(++$i < 100){ if($i%5 == 0){ msg_send($mesg_id, 1, "Hi: Now Index is :". $i); } } } //msg_remove_queue($mesg_id);
Process got a new incoming MSG: s:19:"Hi: Now Index is :5"; Process got a new incoming MSG: s:20:"Hi: Now Index is :10"; Process got a new incoming MSG: s:20:"Hi: Now Index is :15"; Process got a new incoming MSG: s:20:"Hi: Now Index is :20"; Process got a new incoming MSG: s:20:"Hi: Now Index is :25"; Process got a new incoming MSG: s:20:"Hi: Now Index is :30"; Process got a new incoming MSG: s:20:"Hi: Now Index is :35"; Process got a new incoming MSG: s:20:"Hi: Now Index is :40"; Process got a new incoming MSG: s:20:"Hi: Now Index is :45"; Process got a new incoming MSG: s:20:"Hi: Now Index is :50"; Process got a new incoming MSG: s:20:"Hi: Now Index is :55"; Process got a new incoming MSG: s:20:"Hi: Now Index is :60"; Process got a new incoming MSG: s:20:"Hi: Now Index is :65"; Process got a new incoming MSG: s:20:"Hi: Now Index is :70"; Process got a new incoming MSG: s:20:"Hi: Now Index is :75"; Process got a new incoming MSG: s:20:"Hi: Now Index is :80"; Process got a new incoming MSG: s:20:"Hi: Now Index is :85"; Process got a new incoming MSG: s:20:"Hi: Now Index is :90"; Process got a new incoming MSG: s:20:"Hi: Now Index is :95";
sem_acquire -- Acquire a semaphore sem_get -- Get a semaphore id sem_release -- Release a semaphore sem_remove -- Remove a semaphore
function memoryUsage(){ printf("%s: %s<br/>", date("H:i:s", $now), memory_get_usage()); //var_dump(debug_backtrace()); //var_dump(__FUNCTION__); //debug_print_backtrace(); } register_tick_function("memoryUsage"); declare(ticks=1){ $shm_key = ftok(__FILE__, 's'); $shm_id = shmop_open($shm_key, 'c', 0644, 100); } printf("Size of Shared Memory is: %s<br/>", shmop_size($shm_id)); $shm_text = shmop_read($shm_id, 0, 100); eval($shm_text); if(!empty($share_array)){ var_dump($share_array); $share_array['id'] += 1; }else{ $share_array = array('id' => 1); } $out_put_str = "$share_array = " . var_export($share_array, true) .";"; $out_put_str = str_pad($out_put_str, 100, " ", STR_PAD_RIGHT); shmop_write($shm_id, $out_put_str, 0); ?>
PHP Chinese website!
The above is the detailed content of What you may not know about PHP – PHP's event-driven design. For more information, please follow other related articles on the PHP Chinese website!