PHP Backend Socket Service Long link, multiple concurrent development Memo _php Tutorial

Source: Internet
Author: User
Tags php error usleep yii
[PHP]
Guaranteed child Process Upper limit
if ($this->_maxfork >0 && $this->_children > $this->_maxfork)
{
Yii::log ("_children >". $this->_maxfork,clogger::level_warning,__method__);
$this->handler (SIGCHLD);
Usleep (200);
Continue
}


/**
* Monitoring Signal
* @param object Socket $clientt
* @return Boolean
*/
Public Function Handler ($signo) {

Yii::log ("Handler {$signo}", Clogger::level_info, __method__);

Switch (intval ($signo)) {
Case SIGCLD:
Case SIGCHLD:

Yii::log ("SIGCHLD Sub proccess", Clogger::level_trace, __method__);
Normal exit
DECLARE = 1, that means one signal is correspond multi-process die
while ($pid = pcntl_wait ($status, wnohang| wuntraced)) > 0) {
if (FALSE = = = pcntl_wifexited ($status)) {
Yii::log ("Sub proccess {$pid} exited unormally with code {$status}", clogger::level_warning, __method__);
} else {
Yii::log ("Sub proccess {$pid} exited normally", Clogger::level_info, __method__);
}
$this->_children--;
}
Break
Case SIGINT:
Case Sigquit:
Case SIGHUP:
Exception exit
$this->_cleanup ();
Exit (0);
Break
Default
Break
}
}

Guaranteed child Process Upper limit
if ($this->_maxfork >0 && $this->_children > $this->_maxfork)
{
Yii::log ("_children >". $this->_maxfork,clogger::level_warning,__method__);
$this->handler (SIGCHLD);
Usleep (200);
Continue
}


/**
* Monitoring Signal
* @param object Socket $clientt
* @return Boolean
*/
Public Function Handler ($signo) {

Yii::log ("Handler {$signo}", Clogger::level_info, __method__);

Switch (intval ($signo)) {
Case SIGCLD:
Case SIGCHLD:

Yii::log ("SIGCHLD Sub proccess", Clogger::level_trace, __method__);
Normal exit
DECLARE = 1, that means one signal is correspond multi-process die
while ($pid = pcntl_wait ($status, wnohang| wuntraced)) > 0) {
if (FALSE = = = pcntl_wifexited ($status)) {
Yii::log ("Sub proccess {$pid} exited unormally with code {$status}", clogger::level_warning, __method__);
} else {
Yii::log ("Sub proccess {$pid} exited normally", Clogger::level_info, __method__);
}
$this->_children--;
}
Break
Case SIGINT:
Case Sigquit:
Case SIGHUP:
Exception exit
$this->_cleanup ();
Exit (0);
Break
Default
Break
}
}

"Multi-process approach Note point"


Variable sharing issues
Because it is a process, can be understood as a copy of the main process, the execution is from the call Pcntl_fork () after the main, sub-process from the back to run, to avoid the sub-process layer nesting, the general sub-process after the execution of exit (0) to exit. A child process that crashes during execution does not affect the main process, nor can it share variables with the main process.


Signal and select conflict issues
Pcntl_signal will be in conflict with Stream_select after registering the signal.
PHP error[2]: Stream_select (): Unable to select [4]: Interrupted system call (max_fd=10)
No solution has been found, and pcntl_signal is not being used for monitoring.


Process Recycling
When a child process exits through exit, it becomes a zombie process and needs to be reclaimed by pcntl_wait.
The maximum process limit found in the CentOS test is 32,000, which is recycled by setting the total number of child processes, greater than the set value, and then calling Pcntl_wait


Socket read/write note
In the case of multiple processes, there is a problem with the same socket handle, with multiple processes simultaneously reading.
The workaround is that the read operation is in the main process, and after all the data is read out, it is thrown to the child process for execution.


File Operation Note
The FileSize function does not change the size of the file that is not being taken or taken in the multi-process situation.
This is solved by calling the system functions directly under Linux.


$file _size = @filesize ($logFile);
Resolving file size Issues when concurrency is not in progress
if (0 = = $file _size | | $this->_prevfilesize = = $file _size)
{
$file _size = @exec ('/usr/bin/stat-c%s '. Escapeshellarg ($logFile));
Clearstatcache ();
}


1000 of the concurrency after the stress test can reach 350 requests per second. should also be optimized.

http://www.bkjia.com/PHPjc/477526.html www.bkjia.com true http://www.bkjia.com/PHPjc/477526.html techarticle [PHP]//guaranteed child process upper bound if ($this-_maxfork 0 $this-_children $this-_maxfork) {yii::log (_children. $this-_maxfork,clogger:: LEVEL_WARNING,__METHOD__); $this-handler (SIGCHLD); U ...

  • 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.