PHP pcntl Multi-Process Usage Example _php Tutorial

Source: Internet
Author: User

PHP's PCNTL Multi-process Usage example


This article mainly introduced the PHP pcntl multi-process usage, the example analyzes the PCNTL operation multi-process use skill, very has the practical value, needs the friend to refer

The example in this paper describes the PCNTL multi-process usage of PHP. Share to everyone for your reference. The specific analysis is as follows:

PHP uses the PCNTL series of functions to do a multi-process processing of 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.

Suppose 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:

?

1

2

3

4

5

6

7

8

9

Ten

One

+

All

+

+

2 0

(

)

,

,

,

$max = 800000;

$workers =;

$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 the pcntl_fork comes out, it returns a PID value, which is 0 in the child process, the PID (>0) of the subprocess in the parent process, and if the PID is 1, the fork is wrong.

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

I hope this article is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/970755.html www.bkjia.com true http://www.bkjia.com/PHPjc/970755.html techarticle PHP pcntl Multi-Process Usage example this article mainly introduces the PHP pcntl multi-process usage, the example analyzes the PCNTL operation multi-process use skill, very has the practical value, needs the friend to be able ...

  • 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.