Home > Backend Development > PHP Tutorial > Overview of the implementation and optimization of PHP program-level daemon_PHP tutorial

Overview of the implementation and optimization of PHP program-level daemon_PHP tutorial

WBOY
Release: 2016-07-21 15:10:28
Original
909 people have browsed it

The first thing that needs to be explained is what a daemon is.

A daemon process is a process that always runs in the background. For example, the processes we start, such as httpd and mysqld, are all programs running in resident memory.

Analyze needs:

Requirement: There is a resident queue messageQueue (assumed to be in redis memory). This queue may have requests to add elements to the queue from time to time. At the same time, we require that when there are elements in the queue, the elements are popped out according to the queue order and processed (assuming that this processing is just echo 'test');

Solution:

Now assume there are two functions

function oPopMessageQueue(){ …} //Get the last element of the queue;

function vDealElement($element) { …} handles elements;

Required to write a daemon to complete the above requirements.

Program:

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

Copy the code The code is as follows:

while(true)
{
if( $element = oPopMessageQueue())
{
vDealElement($element);
}
}

Consideration 1: If this program is kept running, it can already meet the above needs.

However, consider: 1. Running the php process may cause the process to hang due to various circumstances (such as running for too long), so that the program cannot automatically reconnect.

Method: Use cron

We start a process in the timed script to run this program every 10 minutes.

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

Copy the code The code is as follows:

while(true)
{
if($element = oPopMessageQueue())
{
vCheckTimeLimit();
vDealElement($elemnt);
}
}

$timeStart = 0;
function vCheckTimeLimit()
{
global $timeStart;
if(empty($timeStart))
{
          $timeStart = time();
    }                                                                                                             🎜>


Consider 2,
There may be this demand: You need to have the function of pausing the script at any time:

So consider using a file to add a pause function

Copy code
The code is as follows:

while(true){ if($element = oPopMessageQueue())                                vCheckTimeLimit(); 🎜> { if(file_exists("/home/JesephYe/end"))
{
exit;
}
}



Consider 3,
Can it be changed to a multi-threaded program to run more efficiently?

Just change the cron limit of one process every 10 minutes to one process every 1 minute

This ensures that there are 10 threads running the program

But there is a basic requirement: oPopMessageQueue() is an atomic operation





http://www.bkjia.com/PHPjc/327061.html

www.bkjia.comtrue

http: //www.bkjia.com/PHPjc/327061.html

TechArticle

The first thing that needs to be explained is what a daemon is. A daemon is a process that always runs in the background. For example, the processes we start, such as httpd and mysqld, are all programs running in resident memory. ...

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