PHP Event-Driven design detailed _php tips

Source: Internet
Author: User
Tags message queue semaphore

This example describes the PHP event-driven design. Share to everyone for your reference, specific as follows:

Recently in a need to use asynchronous PHP project, browsing the PHP source, found three of the modules have not been used, sysvsem,sysvshm,sysvmsg, a study, the benefit is not shallow.

There are such a family of functions in PHP that they are the packaging of the V IPC function family of Unix.
They are rarely used by people, but they are powerful. Use them skillfully to make you more than you can.

They include:

Signal Volume (semaphores)
Share memory (Shared memory)
interprocess communication (inter-process messaging, IPC)

Based on these, it is entirely possible to package PHP as 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) to a shaped key used to use System V IPC

2. Ticks

Ticks is added to PHP from PHP 4.0.3, which is an event that happens every time an interpreter executes n low-level statements in a declare code snippet. The value of n is specified by ticks=n in the directive portion of the 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 now use an example to illustrate, how to combine ticks to implement the message communication of PHP.

$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 <) {
  if ($i%5 = = 0) {
msg_send ($mesg _id, 1, "Hi:now index is:". $i);
}} Msg_remove_queue ($MESG _id);

In this example, we first add our PHP execution process to a message queue obtained by the key generated by Ftok.

Then, through the ticks, there are no two statements to query the message queue.

It then simulates message sending.

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

See here is not that everyone has a concept of how to simulate PHP for event-driven already? Don't worry, we'll continue to improve.

2. Signal Volume

The concept of semaphores, we should all be very familiar with. Through the signal volume, can realize process communication, competition and so on. No more details, just a simple list 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

Specific information, you can read the PHP manual.

3. Memory sharing

PHP SYSVSHM provides a memory sharing solution: SYSVSHM, it is and sysvsem,sysvmsg a series, but here I don't use it, I use the SHMOP series function, combine 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__, ' s ');
$shm _id = Shmop_open ($shm _key, ' C ', 0644, M);
}
printf ("Size of shared memory is:%s<br/>", Shmop_size ($shm _id));
$shm _text = Shmop_read ($shm _id, 0);
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, M, "", str_pad_right);
Shmop_write ($shm _id, $out _put_str, 0);
>

Run this example, keep refreshing and we can see the index in increments.

Using this shmop alone, you can complete the function of sharing data between PHP scripts: And, for example, caching, counting, and so on.

For more information about PHP interested readers can view the site topics: "Php Curl Usage Summary", "PHP Socket Usage Summary", "PHP Network Programming Skills Summary", "PHP object-oriented Programming Program Introduction", "PHP Array" operation Skills Encyclopedia, " Summary of PHP string usage, Introduction to PHP+MYSQL database operations, and a summary of PHP common database operations Tips

I hope this article will help you with the PHP program design.

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.