Sharing PHP-PCNTL implementing multi-Process Code _php instance

Source: Internet
Author: User

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.

Now let's take a look at an example

Code

<?php
$arChildId = Array ();

for ($i = 0; $i < $i + +)
{
$iPid = Pcntl_fork ();
if ($iPid = = = 1)
{
  die (' can\ ' t be forked. ');
}

if ($iPid)
{
  # main process logic
  $arChildId [] = $iPid;
}
else
  {
  # subprocess logic
  $iPid = Posix_getpid (); # Gets the ID of the child process
  $iSeconds = rand (5);
  Echo ' * Process '. $iPid. ' is created, and executed, and sleep '. $iSeconds. Php_eol;
  Excuteprocess ($iPid, $iSeconds);
  Exit ();
}

while (count ($arChildId) > 0)
{
foreach ($arChildId as $iKey => $iPid)
{
  $res = Pcntl_waitpid ( $iPid, $status, Wnohang);

  if ($res = = 1 | | $res > 0)
  {
    unset ($arChildId [$iKey]);
    Echo ' * Sub process: '. $iPid. ' Exited with '. $status. Php_eol
}

}} # The logical
function excuteprocess ($iPid, $iSeconds) performed by the subprocess
{
file_put_contents ('./log/'. $iPid. Log ', $iPid. Php_eol, file_append);
Sleep ($iSeconds);
>

Run results

* Process 16163 is created, and executed, and sleep-through
process 16164 was created, and executed, and sleep
* Process 16165 is created, and executed, and sleep
* Process 16166 is created, and executed, and sleep
* Pr Ocess 16167 is created, and executed, and sleep 8
* Process 16168 is created, and executed, and sleep
* PROCE  SS 16169 is created, and executed, and sleep
* Process 16170 is created, and executed, and sleep
* process 16171 was created, and executed, and the sleep
Process 16172 is created, and executed, and sleep
* SUB proc Ess:16167 exited with 0
* Sub process:16163 exited with 0
* Sub process:16169 exited with 0
* Sub proces  S:16168 exited with 0
* Sub process:16171 exited with 0
* Sub process:16164 exited with 0
* Sub process: 16172 exited with 0
* Sub process:16165 exited with 0
* Sub process:16170 exited with 0
* Sub process:1 6166 exited with 0

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.