PHP uses pcntl_fork to implement multi-process downloading of images, _PHP tutorial

WBOY
Release: 2016-07-13 10:11:41
Original
976 people have browsed it

PHP uses pcntl_fork to implement multi-process downloading of images,

The example in this article describes how PHP uses pcntl_fork to implement multi-process downloading of images. Share it with everyone for your reference. The specific analysis is as follows:

PHP pcntl_fork — Generates a branch, a child process, at the current position of the current process. Annotation: fork creates a child process. Both the parent process and the child process continue to execute from the fork position. The difference is that during the execution of the parent process , the fork return value obtained is the child process number, and the child process gets 0.

Note: PHP has a pcntl_fork function that can implement multi-process, but it needs to load the pcntl extension, and this extension can only be compiled under Linux.

1. First compile pcntl.so under ubuntu. I couldn’t find the pcntl package under my ubuntu, so I created a folder and downloaded the entire PHP package. I found the pcntl package in it and ran the following command. The code is as follows:

Copy code The code is as follows:
# mkdir php
# cd php
# apt-get source php5
# cd php5-(WHATEVER_RELEASE)/ext/pcntl
# phpize
# ./configure (Note 1)
# make
# The make install phpize command is used to prepare the compilation environment for PHP plug-in modules

A successful installation will create extname.so and place it in the plug-in module directory of PHP (default stored in /usr/lib/php/modules/). You need to adjust php.ini and add extension=extname.so. This plug-in module can only be used after one line.

Example:

Copy code The code is as follows:
void pcntl_exec(string $path [,array $args [,array $envs ]])

pcntl_exec — Execute the specified program in the current process space, the code is as follows:

Copy code The code is as follows:
$cmds=array(
array('/home/jerry/projects/www/test2.php'),
array('/home/jerry/projects/www/test3.php')
);

foreach($cmds as $cmd){
          $pid=pcntl_fork();
If($pid==-1){
//Process creation failed
                                            echo 'Return -1 when creating a child process fails';
exit(-1);
          }
          else if($pid){
//The parent process will get the child process number, so here is the logic executed by the parent process
pcntl_wait($status,WNOHANG);
          }
         else{
//Subprocess processing logic
               sleep(5);
​​​​​​ pcntl_exec('/usr/bin/php',$cmd);
exit(0);
          }
}

For example, to download multiple images simultaneously, the code is as follows:
Copy code The code is as follows:
#!/usr/bin/php
//Web page address to be crawled
$url = 'http://www.bkjia.com';
$content = file_get_contents($url);
preg_match_all('/ echo "Found".count($matches)."Pictures n";

list($sm, $ss) = explode(" ", microtime());
foreach ($matches as $k => $val)
{
$pid[$k] = pcntl_fork();
if(!$pid[$k])
{
download($url, $val);
// The child process must exit, otherwise it will perform recursive multi-processing. The parent process must not exit, otherwise it will terminate the multi-process
exit(0);
}

if ($pid[$k])
{
// pcntl_waitpid($pid[$k], $status, WUNTRACED);
}

}
echo "Download completedn";

list($em, $es) = explode(" ", microtime());

echo "Time taken:",($es+$em) - ($ss + $sm),"n";
/**
* Grab web images
*
​*/
function download($url, $val)
{
$pic_url = $val[1];
if (strpos($val[1], '//') !== false)
{
;
}
elseif (preg_match('@^(.*?)/@', $val[1], $inner_matches) == 0)
{
$pic_url = $url.$val[1];
}
elseif (preg_match('@[:.]@', $inner_matches[1], $tmp_matches) == 0)
{
$pic_url = $url.$val[1];
}

$pic = file_get_contents($pic_url);

if ($pic === false)
{
Return;
}

preg_match('@/([^/]+)$@', $pic_url, $tmp_matches);
// You can use assert to handle exceptions
$pic_file_name = $tmp_matches[1];
$f = fopen("tmp/".$pic_file_name, "wb"); #
fwrite($f, $pic);
fclose($f);
}

/* End of file pcntl_fork.php */
?>

I hope this article will be helpful to everyone’s PHP programming design.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/928221.htmlTechArticlePHP uses pcntl_fork to implement multi-process downloading of images. This article describes the example of PHP using pcntl_fork to implement multi-process downloading of images. method. Share it with everyone for your reference. Detailed analysis...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!