PHP Install PCNTL extensions implement multiple processes

Source: Internet
Author: User

Pcntl the extensions that the PHP implementation multi process must install, the extended installation steps are now written below.

One or two kinds of installation methods

1, recompile PHP's back configrue hint plus? enable-pcntl.
2, do not recompile PHP, direct compilation installation Pcntl extension.

# cd/usr/local/src/php-5.2.6/ext/pcntl #

/usr/local/php/bin/phpize #

/configure? with-php-config=/usr/ Local/php/bin/php-config

# make && make install

You can then add pcntl.so to php.ini and use the Php-m View Module command to view the installed modules.

Second, the example

For ($x  = 1; $x <= 2; $x + +) {         $pid [$x] =

 pcntl_fork ();         if  ($pid [$x] == -1)  {   

             die ("Could not fork");         } elseif  ($pid [$x])  {                 echo  "parent: create ". $pid [$x]. "

n ";         } else {                         echo   "fork ". Getmypid (). "

 start:n ";                          for ($i  = 0; $i <10; $i + +) {                                  echo  $x. ": ". $i. "

n ";                   

              sleep (1);                          }             

    exit;        &NBSP}}

This article is an example of PHP's PCNTL multi-process usage. Share to everyone for your reference. The specific analysis is as follows:

PHP uses the PCNTL series functions to do multiple processes to handle a transaction. For example, I need to get 80w data from the database, and then do a series of subsequent processing, this time, with a single process? You can wait until next year today. So you should use the PCNTL function.

Let's say I want to start 20 processes, divide the 1-80w data into 20 parts, and the main process waits for all the child processes to end before exiting:

$max  = 800000;

$workers  = 20;

  $pids  = array (); For ($i  = 0;  $i  <  $workers;  $i + +) {     $pids [$i] =

 pcntl_fork ();     switch  ($pids [$i])  {        case  -1:             echo  "Fork error

 : {$i} \r\n ";

            exit;         case 0:               $param  = array (                  ' LastID '  =>  $max  /  $workers  *  $i,                   ' Maxid '  =>   $max  /  $workers  *  ($i + 1),             );              $this->executeworker ($input,  $

output,  $param);

            exit;         default:          

   break; &NBSP;&NBSP;&NBSP;&NBSP}   foreach  ($pids  as  $i  =>  $pid)  {  &

Nbsp; if ($pid)  {        pcntl_waitpid ($pid,  $status); &NBSP;&NBSP;&NBSP;&NBSP}}

Here when pcntl_fork out, will return a PID value, this PID in the child process see is 0, in the parent process is a child process PID (>0), if the PID 1 indicates that fork error.

Using a $pids array allows the main process to wait for all processes to end.

Example Test

PCNTL expansion of the installation will not say, very simple, here in conjunction with the example to say how pcntl_fork operation

<?php

echo $pid = Pcntl_fork ();

if ($pid = = 1) {

die (' could not fork ');

else if (! $pid) {

//Here is the child process

echo '-';

Exit ();

}else{

//Here is the parent process

echo ' A ';

echo ' B '

; }

The result of my running multiple times with the CLI is

The

$pid 0 is a subprocess, because Pcntl_fork () is the function of trying to create a child process when the program is running here, and if the result is created, then the subprocess that returns the current process id,0 that the current process does not have a subprocess, then the process itself is not a subprocess. If there is a process ID that indicates that it has a child process, then it is not the parent process. As if it were from this location, all of the following code was replicated to execute in another process, resulting in a child process. But from the results of the above multiple execution, it is most of the time to execute the subprocess, which is a bit different from the data I checked on the Internet, and this order is not fixed. But we found that the last and third article is the first execution of the parent process, followed by the child process, which has a certain risk, although the child process will not die, because he will adopt to the 1 process, but if you want to wait for the child process to finish can do so:
 

<?php   echo  $pid = pcntl_fork ();   if  ($pid  == -1)  {       die (' could not  Fork ');    } else if  (! $pid)  {   //This is a subprocess   echo
  '-';
  exit ();   }else{  //This is the parent process  echo  ' A ';   pcntl_wait ($status);
The parent process executes execution  echo  ' B ' after the child process is executed here and so on; &NBSP;&NBSP}   Run results:   0-31843ab[root@client 60.test.com]# php index.php  0-31845AB[ root@client 60.test.com]# php index.php  0-31847ab[root@client 60.test.com]# php  index.php  0-31849ab[root@client 60.test.com]# php index.php  0-31851ab[ root@client 60.test.com]# php index.php  0-31853ab[root@client 60.test.com]# php  index.php  0-31855ab[root@client 60.test.com]# php index.php  31857a0-b[ root@client 60.test.com]# php index.php  0-31859ab[root@client 60.test.com]# php index.php   0-31861ab[root@client 60.test.com]# php index.php  0-31863ab[root@client 60.
test.com]# php index.php  0-31865ab[root@client 60.test.com]# php index.php  0-31867ab[root@client 60.test.com]# php index.php  0-31869ab[root@client 60.test.com]#  php index.php  0-31871ab[root@client 60.test.com]# php index.php  0-31873ab[ root@client 60.test.com]# php index.php  0-31875ab[root@client 60.test.com]# php  index.php  0-31877ab[root@client 60.test.com]# php index.php  0-31879ab[ root@client 60.test.com]# php index.php  0-31881ab[root@client 60.test.com]# php  index.php  0-31883ab[root@client 60.test.com]# php index.php

It's the result we want, and the rest is fine, because it's a subprocess that executes first, and that's the one that executes the parent process, and then it goes to output a, which is done after the subprocess executes. So this is

Ntl_wait ($status)

's role.

Well, that's basically it. Communication between processes and threads can be done using a similar shared memory variable. Specific situation to treat it.

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.