PHP Event-Driven design

Source: Internet
Author: User
Tags php source code
This article mainly introduces the PHP event-driven design, now share to everyone, the need for friends can refer to the next

An example of PHP event-driven design is described in this article. Share to everyone for your reference, as follows:

Recently in a need to use asynchronous PHP project, flipping through PHP source code, found three unused modules, sysvsem,sysvshm,sysvmsg, after a study, the benefits are 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 seldom used by people, but they are very powerful. Clever use of them, can make you more effort.

They include:

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

Based on this, it is entirely possible for us to package PHP into a message-driven system.

But first, we need to introduce a few important foundations:

1. Ftok

int Ftok (String pathname, String proj)

Ftok converts a pathname pathname and a project name (which must be a character) into a shaped key used by the System V IPC

2. Ticks

Ticks is added to PHP from PHP 4.0.3, which is an event that occurs every time an interpreter executes n low-level statements in a declare code snippet. The value of n is specified in the directive section of declare with Ticks=n.

function GetStatus ($arg) {  print_r (Connection_status ());  Debug_print_backtrace ();} Reigster_tick_function ("GetStatus", True);d Eclare (Ticks=1) {for  ($i =1; $i <999; $i + +) {echo "Hello";}} Unregister_tick_function ("GetStatus");

This is basically the equivalent of:

function GetStatus ($arg) {  print_r (Connection_status ());  Debug_print_backtrace ();} Reigster_tick_function ("GetStatus", True);d Eclare (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 PHP message communication.

$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 isn't Ready"),} if (Msg_receive ($MESG _id, 0, $MESG _type, 1024x768, $MESG, False , msg_ipc_nowait)) {  Print_r ("Process got a new incoming msg: $MESG");}} Register_tick_function ("Fetchmessage", $mesg _id);d eclare (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 the message queue obtained by the key generated by Ftok.

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

The message is then simulated for sending.

To access this script in the browser, the results are as follows:

Process got a new incoming msg:s:19: "Hi:now index Is:5";p Rocess got a new incoming msg:s:20: "Hi:now index is:10";p ro Cess got a new incoming msg:s:20: "Hi:now index is:15";p Rocess got a new incoming msg:s:20: "Hi:now index is:20";p ROCE  SS got a new incoming msg:s:20: "Hi:now index is:25";p Rocess got a new incoming msg:s:20: "Hi:now index is:30";p rocess Got a new incoming msg:s:20: "Hi:now index is:35";p Rocess got a new incoming msg:s:20: "Hi:now index is:40";p rocess G  OT a new incoming msg:s:20: "Hi:now index is:45";p Rocess got a new incoming msg:s:20: "Hi:now index is:50";p rocess got  A new incoming msg:s:20: "Hi:now index is:55";p Rocess got a new incoming msg:s:20: "Hi:now index is:60";p Rocess got A New incoming msg:s:20: "Hi:now index is:65";p Rocess got a new incoming msg:s:20: "Hi:now index is:70";p Rocess got a n  EW incoming msg:s:20: "Hi:now index is:75";p Rocess got a new incoming msg:s:20: "Hi:now index is:80";p Rocess got a new Incoming msg:s:20: "Hi: Now index is:85 ";p Rocess got a new incoming msg:s:20:" Hi:now index is:90 ";p Rocess got a new incoming msg:s:20:" Hi:n ow index is:95 ";

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

2. Signal Volume

The concept of semaphore, everyone should be very familiar with. Through the signal volume, process communication, competition and so on can be realized. Again, it's just a simple list of semaphore function sets 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 scheme: SYSVSHM, which is and sysvsem,sysvmsg a series of, but here, I did not use it, I use the SHMOP series functions, combined 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");d Eclare (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, +), 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, +, "", Str_pad_right) shmop_write ($shm _id, $out _put_str, 0);? >

To run this example, keep refreshing and we can see that index is incrementing.

Using this shmop alone can accomplish the ability to share data between PHP scripts: as well, such as caching, counting, and so on.

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.