Home  >  Article  >  Backend Development  >  What you may not know about PHP – PHP’s event-driven design

What you may not know about PHP – PHP’s event-driven design

angryTom
angryTomforward
2019-10-15 11:20:132024browse

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. ftok

int ftok ( string pathname, string proj )
//ftok将一个路径名pathname和一个项目名(必须为一个字符), 转化成一个整形的用来使用系统V IPC的key

2. ticks

Ticks is from PHP 4.0. It was only added to PHP at the beginning of 3. It is an event that occurs every time the interpreter executes N low-level statements in the declare code segment. The value of N is specified with ticks=N in the directive part of declare.

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");

This is basically equivalent to:


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");

Message. I will now use an example to illustrate how to combine Ticks to implement PHP message communication.

$mesg_key = ftok(__FILE__, &#39;m&#39;);
$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);

In this example, first add our PHP execution Process to a message queue obtained by the Key generated by ftok.

Then, through Ticks, query the message queue once every two statements.

Then simulated message sending.

Access this script in the browser, the result is as follows:

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";

Seeing this, everyone already has a concept of how to simulate PHP as event-driven? Don't worry, we will continue to improve.

3. Semaphore

Everyone should be familiar with the concept of semaphore. Through semaphores, process communication, competition, etc. can be achieved. I won’t go into details again, but simply list the set of semaphore functions provided in PHP.

sem_acquire -- Acquire a semaphore
sem_get -- Get a semaphore id
sem_release -- Release a semaphore
sem_remove -- Remove a semaphore

For specific information, you can read the PHP manual.

4. Memory sharing

PHP sysvshm provides a memory sharing solution: sysvshm, which is in the same series as sysvsem and sysvmsg, but here, I did not use it, I used shmop series functions, run this example in conjunction with TIcks

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__, &#39;s&#39;);
 
$shm_id = shmop_open($shm_key, &#39;c&#39;, 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[&#39;id&#39;] += 1;
 
}else{
 
 $share_array = array(&#39;id&#39; => 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);
 
?>

, and continuously refresh, we can see that the index is increasing.

Just using this shmop can complete the functions of sharing data between PHP scripts: as well as, such as caching, counting, etc.

For more PHP related knowledge, please visit

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!

Statement:
This article is reproduced at:www.laruence.com. If there is any infringement, please contact admin@php.cn delete

Related articles

See more