Multi-process application analysis in PHPCLI mode _ PHP Tutorial

Source: Internet
Author: User
Tags php web development
Multi-process application analysis in PHPCLI mode. In many cases, PHP is not suitable for resident SHELL processes. it does not have dedicated gc routines or effective memory management channels. therefore, if you use PHP as the resident SHELL, you will often be exhausted by the memory to guide PHP. in many cases, it is not suitable for resident SHELL processes. he does not have a dedicated gc routine, there is no effective memory management method. so if you use PHP as the resident SHELL, you will often be exhausted by memory, resulting in abort and unhappy.

In addition, if the input data is invalid and the script does not detect it, resulting in abort, it will make you very unhappy.

So? What should we do?

Multi-process ....

Why?

Advantages:
1. when a multi-process is used, the kernel will be responsible for recycling resources after the sub-process ends.
2. if a multi-process is used, abnormal exit of the sub-process will not cause the Thread of the whole process to exit. the parent process also has the opportunity to rebuild the process.
3. a resident process is only responsible for task distribution, and the logic is clearer.

Then, how to do it?

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

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

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 run in SHELL */
If (substr (php_sapi_name (), 0, 3 )! = 'Cli '){
Die ("This Programe can only be run in CLI mode ");
}

/** Disable the maximum execution time limit. in CLI mode, this statement is not necessary */
Set_time_limit (0 );

$ Pid = posix_getpid (); // Obtain the master process ID
$ User = posix_getlogin (); // obtain the 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 a sub-process
If ($ pid = 0) {// sub-process
$ Pid = posix_getpid ();
Echo "* Process {$ pid} was created, and Executed: \ n ";
Eval ($ input); // resolution command
Exit;
} Else {// master process
$ Pid = pcntl_wait ($ status, WUNTRACED); // get the sub-process end status
If (pcntl_wifexited ($ status )){
Echo "\ n * Sub process: {$ pid} exited with {$ status }";
}
}
}


However, I must remind you that:

The code is as follows:


Process Control shoshould 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, that is to say, to eliminate your idea of using multiple processes in PHP Web development!



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

There is no dedicated gc routine, and there is no effective memory management path. so if you use PHP for resident SHELL, you will often be exhausted by the memory...

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.