PHP Program-level daemon implementation and optimization overview _ php instance

Source: Internet
Author: User
This article describes how to implement and optimize the PHP Program-level daemon. For more information, see what the daemon process is.

A daemon is a process that has been running in the background. For example, the httpd and mysqld processes we started are all programs running in the resident memory.

Analyze the requirements:

Requirement: There is a resident queue messageQueue (assuming it is in redis memory), this queue may have requests occasionally adding elements to the queue. At the same time, when there are elements in the queue, We need to pop out the elements in the queue order and process them (assuming this process is only echo 'test ');

Solution:

Now we assume that we already have two functions.

Function oPopMessageQueue (){...} // Obtain the last element of the queue;

Function vDealElement ($ element ){...} Processing element;

Write a daemon to fulfill the preceding requirements.

Program:

Well, this program is easy to think of. You can use the while loop to do it.

The Code is as follows:


While (true)
{
If ($ element = oPopMessageQueue ())
{
VDealElement ($ element );
}
}


Consideration 1:If this program has been running, it can already meet the above requirements.

However, for example, if a php process is used for running, the process may fail due to various circumstances (for example, the running time is too long), so that the program cannot be automatically reconnected.

Method: Use cron

We run this program every 10 minutes in the scheduled script.

Then set the running time of this program to 10 minutes, and automatically cancel the program after 10 minutes, so the code becomes

The Code is as follows:


While (true)
{
If ($ element = oPopMessageQueue ())
{
VCheckTimeLimit ();
VDealElement ($ elemnt );
}
}

$ TimeStart = 0;
Function vCheckTimeLimit ()
{
Global $ timeStart;
If (empty ($ timeStart ))
{
$ TimeStart = time ();
}

If (time ()-$ timeStart> 60*10)
{
Exit;
}
}


Consideration 2,There may be such a requirement: the script must be paused at any time:

So we want to use files to increase the pause function.

The Code is as follows:


While (true)
{
If ($ element = oPopMessageQueue ())
{
VCheckTimeLimit ();
VCheckEnd ();
VDealElement ($ elemnt );
}
}


Function vCheckEnd ()
{
If (file_exists ("/home/JesephYe/end "))
{
Exit;
}
}


Consideration 3,Can I change it to a multi-threaded program to improve the running efficiency?

In this case, you only need to change the limit of a process starting from 10 minutes of cron to one process starting every minute.

This ensures that 10 threads are running the program.

However, there is a basic requirement that oPopMessageQueue () is an atomic operation.

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.