Multi process application analysis in PHP CLI mode

  • 2020-06-07 04:04:18
  • OfStack

There are many times when PHP is not a good place to be a resident SHELL process, it doesn't have a dedicated gc routine, and it doesn't have an efficient way to manage memory, so if you use PHP as a resident SHELL, you will often run out of memory and end up with abort and unhappy.

Also, if the input data is illegal and the script is not detected, resulting in abort, you will also be unhappy.

The? What to do?

Multiple processes... .

Why is that?

Advantages:
1. Using multiple processes, the kernel takes care of recycling resources after the child process is finished
2. With multiple processes, the exception exit of the child process does not result in the exit of the entire process, Thread.
3. One resident main process, only responsible for task distribution, with clearer logic

Then, how do you do that?

Next, we use the POSIX and Pcntl series of functions provided by PHP to implement an PHP command parser. The main process is responsible for accepting user input, then the fork child process executes, and is responsible for echo the end state of the child process.

The code is as follows, I added comments, if you do not understand the place, you can read the manual related functions, or reply to the message.

 
#!/bin/env php 
<?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<laruence@baidu.com> 
* @license http://www.zend.com/license/3_0.txt PHP License 3.0 
* @version 1.0.0 
*/ 

/**  Make sure that this function only runs in SHELL In the  */ 
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 unnecessary  */ 
set_time_limit(0); 

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

echo <<<EOD 
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 child process  
if ($pid == 0) {// The child process  
$pid = posix_getpid(); 
echo "* Process {$pid} was created, and Executed:\n\n"; 
eval($input); // Parse the command  
exit; 
} else {// The main process  
$pid = pcntl_wait($status, WUNTRACED); // Gets the subprocess end status  
if (pcntl_wifexited($status)) { 
echo "\n\n* Sub process: {$pid} exited with {$status}"; 
} 
} 
} 


But there is one thing I must remind you of:
 
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. -- from PHP Hands means ,  Reassure you in PHP Web Use the idea of multiple processes in your development ! 


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


Related articles: