Home > Backend Development > PHP Tutorial > Multi-process application analysis in PHP CLI mode_PHP tutorial

Multi-process application analysis in PHP CLI mode_PHP tutorial

WBOY
Release: 2016-07-21 15:09:47
Original
1035 people have browsed it

PHP is not suitable for being a resident SHELL process in many cases. It does not have a dedicated gc routine and no effective memory management method. Therefore, if you use PHP as a resident SHELL, you will often be aborted due to memory exhaustion. And unhappy.

Moreover, if the input data is illegal and the script does not detect it, resulting in abort, it will also make you very unhappy.

Then? What should I do?

Multi-process….

Why?

Advantages:
1. Using multi-process, after the child process ends, the kernel will be responsible for recycling resources
2 . Using multiple processes, the abnormal exit of the child process will not cause the entire process Thread to exit. The parent process will also have the opportunity to rebuild the process.
3. A resident main process, only responsible for task distribution, the logic is clearer

Then, how to do it?

Next, we use the POSIX and Pcntl series functions provided by PHP to implement a PHP command parser. The main process is responsible for accepting user input, and then the fork child process is executed and responsible for returning Displays the end status of the child process.

The code is as follows. I have added comments. If there is anything you don’t understand, you can read the relevant functions in the manual or reply to the message.

Copy code The code is as follows:

#!/bin/env php
/**A example denoted muti-process application in php
* @filename fork.php
* @touch date Wed 10 Jun 2009 10:25:51 PM CST
* @author Laruence
* @license http://www.zend.com/license/3_0.txt PHP License 3.0
* @version 1.0.0
*/

/**Make sure this function can only be run in SHELL*/
if (substr(php_sapi_name(), 0, 3) !== 'cli') {
die("This Programe can only be run in CLI mode ");
}

/**Turn off the maximum execution time limit. In CLI mode, this statement is actually unnecessary.*/
set_time_limit(0);

$pid = posix_getpid(); //Get the main process ID
$user = posix_getlogin(); //Get user name

echo <<USAGE: [command | expression]
input php code to execute by fork a new process
input quit to exit

Shell Executor version 1.0.0 by laruence
EOD;

while (true) {

$prompt = "n{$ user}$ ";
$input = readline($prompt);

readline_add_history($input);
if ($input == 'quit') {
break;
}
process_execute($input . ';');
}

exit(0);

function process_execute($input) {
$pid = pcntl_fork (); //Create sub-process
if ($pid == 0) {//Sub-process
$pid = posix_getpid();
echo "* Process {$pid} was created, and Executed :nn";
eval($input); //Parse command
exit;
} else {//Main process
$pid = pcntl_wait($status, WUNTRACED); //Get child Process end status
if (pcntl_wifexited($status)) {
echo "nn* Sub process: {$pid} exited with {$status}";
}
}
}


But one thing, I must remind you:
Copy the code The code is as follows:

Process Control should not be enabled within a webserver environment and unexpected results may happen if any Process Control functions are used within a webserver environment. --Excerpted from PHP Handbook In other words, give up the idea of ​​using multiple processes in PHP web development Go!


Original text: http://www.laruence.com/2009/06/11/930.html

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/327195.htmlTechArticlePHP is not suitable to be a resident SHELL process in many cases. It does not have a dedicated gc routine and is not effective. Memory management approach. So if you use PHP as a resident SHELL, you will often be led by memory exhaustion...
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