Php event-driven design details, php event design details

Source: Internet
Author: User

Php event-driven design details, php event design details

This article describes the php event-driven design. We will share this with you for your reference. The details are as follows:

Recently, I am working on a project that requires asynchronous php. When I read the php source code, I found three unused modules, sysvsem, sysvshm, and sysvmsg. After some research, I will not be very helpful.

There are such a family of functions in php, they are packaging the v ipc function family of unix.
They are rarely used, but they are powerful. The clever use of them can make you more than half the effort.

They include:

Semaphores)
Shared memory)
Inter-process communication (ipc)

Based on these, we are likely to package php into a message-driven system.

However, first, we need to introduce several important foundations:

1. ftok

Int ftok (string pathname, string proj)

Ftok converts a pathname and a project name (must be a character) into an integer using the key of the System v ipc.

2. ticks

Ticks is added to php from php 4.0.3. It is an event that occurs when the interpreter executes n low-level statements in the declare code segment. The value of n is specified by ticks = n in the directive section 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:

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 use an example to illustrate how to use ticks to Implement Message Communication in 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 < 100){  if($i%5 == 0){msg_send($mesg_id, 1, "hi: now index is :". $i);  } }}//msg_remove_queue($mesg_id);

In this example, add the php Execution process to the Message Queue obtained by the key generated by ftok.

Then, the message queue is queried once without two separate statements through ticks.

Then simulate the message sending.

Access the script in a browser and 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";

Do you have a idea on how to simulate php as an event-driven system? Don't worry, we will continue to improve.

2. semaphores

The concept of semaphores should be familiar to everyone. Processes can communicate and compete with each other through semaphores. I will not go into details again, but simply list the semaphore function set provided by php.

Sem_acquire -- acquire a semaphore
Sem_get -- get a semaphore id
Sem_release -- release a semaphore
Sem_remove -- remove a semaphore

For more information, see the php manual.

3. Memory Sharing

Php sysvshm provides a Memory Sharing Solution: sysvshm, which is a series of sysvsem and sysvmsg, but I didn't use it here. The shmop series functions I use, 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__, '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);?>

Run this example and refresh it constantly. We can see that the index is increasing progressively.

You can use this shmop to share data among php scripts, such as cache and count.

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.