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