Install pcntl extension in php to implement multi-process-PHP source code

Source: Internet
Author: User
The php in pcntl must be installed with pcntl to implement multithreading. I have found many installation tutorials on pcntl on the Internet. The following describes a complete installation and usage of phppcntl. The php in pcntl must be installed with pcntl to implement multithreading. I have found many installation tutorials on pcntl on the Internet. The following describes a complete article on how to install and use php pcntl.

Script ec (2); script

In pcntl, php must be installed to implement multi-process extensions. The extension installation steps are as follows.

I. Two installation methods

1. Why is the configrue prompt added after PHP recompilation? Enable-pcntl.
2. Directly compile and install the pcntl extension without re-compiling php.

# 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

Add pcntl. so to php. ini. Run the php-m command to view the installed modules.

Ii. Instances

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;        }}

This article describes the usage of pcntl multi-process in PHP. Share it with you for your reference. The specific analysis is as follows:

PHP uses functions of the PCNTL series to process a transaction in multiple processes. For example, if I need to retrieve million pieces of data from the database and perform a series of subsequent processing, how can I use a single process? You can wait until next year. So we should use the pcntl function.

Suppose I want to start 20 processes and divide the data from 1 to 80 W into 20 parts. The main process will exit after all the sub-processes are completed:

$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;    }} foreach ($pids as $i => $pid) {    if($pid) {        pcntl_waitpid($pid, $status);    }}

Here, when pcntl_fork comes out, a pid value will be returned. This pid is 0 in the sub-process, and the pid in the parent process is the sub-process pid (> 0 ), if the pid is-1, the fork has an error.

With a $ pids array, the main process can be completed after all processes are completed.

Example Test

The installation of pcntl extension is not mentioned. It is very simple. Here we will talk about the running method of pcntl_fork in combination with the instance.

 

The result of running cli multiple times is:

$ If the pid is 0, it indicates a sub-process, because pcntl_fork () is used to create a sub-process when the program runs here, then, the child process id of the current process is returned. 0 indicates that the current process has no child process, and the process itself is not a child process. If there is a process id, it indicates that it has a child process, then it is not the parent process. It is like from this location where all the following code is copied to another process for execution, and a sub-process is generated. However, according to the preceding execution results, in most cases, the sub-process is executed first, which is somewhat different from the information I checked online, and the order is not fixed. However, we found that the last and third thread executes the parent process first and then the child process. At this time, the child process is at risk, although the child process will not die, because it will pass through the 1 process, but if you want to wait for the sub-process to complete the execution, you can do this:

  

This is what we want, and there is no problem with others, because they are all sub-processes that are first executed, and this is the parent process that is first executed, go to output A and wait until the sub-process is executed. So this is

ntl_wait($status)

.

Okay, that's all. For communication between processes or threads, you can use shared memory variables. Take specific actions.

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.