php event driven design

不言
Release: 2023-03-23 12:52:02
Original
1701 people have browsed it

This article mainly introduces the PHP event-driven design. Now I share it with you. Friends in need can refer to it.

The examples in this article describe the PHP event-driven design. Share it with everyone for your reference, the details are as follows:

I was recently 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, sysvmsg, After some research, I benefited a lot.

There is such a family of functions in php. They 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 introduce a few important basics:

1. ftok

int ftok ( string pathname, string proj )

ftok converts a pathname pathname and a project name (must be one character) into an integer key used to use system v ipc

2. ticks

ticks were only added to PHP starting from PHP 4.0.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");
Copy after login

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");
Copy after login

Message, I use one now Examples to illustrate how to use 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);
Copy after login

In this example, first add our php execution process to the 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";
Copy after login

See here, everyone has already figured out how to simulate PHP as event-driven Already have a concept? Don't worry, we will continue to improve.

2. 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 semaphore function set 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.

3. Memory sharing

php sysvshm provides a memory sharing solution: sysvshm, which is in the same series as sysvsem and sysvmsg, but here, I do not Without using it, I used the shmop series of functions, combined with ticks

function memoryusage(){
 printf("%s: %s<br/>", date("h:i:s",time()), 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);
?>
Copy after login

to run this example and refresh continuously. 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.

Related recommendations:

Implementation of PHP event mechanism

Detailed analysis: About PHP event-driven issues_PHP tutorial

The above is the detailed content of php event driven design. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!