PHP uses Pcntl_fork to implement a multi-process download picture method, _php tutorial

Source: Internet
Author: User

PHP uses Pcntl_fork to implement a multi-process download picture method,


This article explains how PHP uses Pcntl_fork to implement multi-process download images. Share to everyone for your reference. The specific analysis is as follows:

PHP pcntl_fork-in the current position of the present process to create a branch, sub-process, the fork is the creation of a child process, the parent process and the child process from the position of the fork to continue to execute, different from the parent process, the resulting fork return value is the child process number, and the sub-process gets 0.

Note: PHP has a pcntl_fork function to implement multi-process, but to load pcntl extension, and only under Linux to compile this extension.

1. First compile pcntl.so under Ubuntu, my Ubuntu can't find Pcntl package, so create a folder to download the entire PHP package, found inside the PCNTL package run the following command, the code is as follows:
Copy the Code code as follows: # mkdir PHP
# CD PHP
# Apt-get Source PhP5
# CD php5-(whatever_release)/ext/pcntl
# phpize
#./configure (Note i)
# make
# make install Phpize command is used to prepare the PHP plugin for the build environment

A successful installation will be built extname.so and placed in the PHP plug-in module directory (preset stored in the/usr/lib/php/modules/), need to adjust php.ini, add extension=extname.so You can use this plug-in after this line.

Example: copy code code as follows: void Pcntl_exec (String $path [, array $args [, array $envs]]

pcntl_exec-executes the specified program in the current process space, with the following code:
Copy CodeThe 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 ' failed to create child process returns-1 ';
Exit (-1);
}
else if ($pid) {
The parent process gets the child process number, so this is the logic that the parent process executes
Pcntl_wait ($status, Wnohang);
}
else{
Subprocess Process Logic
Sleep (5);
Pcntl_exec ('/usr/bin/php ', $cmd);
Exit (0);
}
}
example, the example of multi-image synchronization download, the code is as follows:
Copy CodeThe code is as follows: #!/usr/bin/php
<?php
Web address to crawl
$url = ' http://www.bkjia.com ';
$content = file_get_contents ($url);
Preg_match_all ('/echo ' found. Count ($matches). " Picture n ";

List ($SM, $ss) = Explode ("", Microtime ());
foreach ($matches as $k = $val)
{
$pid [$k] = Pcntl_fork ();
if (! $pid [$k])
{
Download ($url, $val);
Child process to exit otherwise a recursive multi-process is performed, and the parent process does not exit or terminate the multi-process
Exit (0);
}

if ($pid [$k])
{
Pcntl_waitpid ($pid [$k], $status, wuntraced);
}

}
echo "Download complete n";

List ($em, $es) = Explode ("", Microtime ());

echo "Spents:", ($es + $em)-($ss + $sm), "n";
/**
* Crawl 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);
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 is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/928221.html www.bkjia.com true http://www.bkjia.com/PHPjc/928221.html techarticle PHP uses pcntl_fork to implement multi-process download Picture method, this article describes the PHP use Pcntl_fork to implement multi-process download picture method. Share to everyone for your reference. Specific analysis ...

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