PHP uses Pcntl_fork to implement multi-process download Picture method _php Tips

Source: Internet
Author: User
Tags explode

This example describes how PHP uses Pcntl_fork to implement a multiple process download picture. Share to everyone for your reference. The specific analysis is as follows:

PHP pcntl_fork-in the current process of the current position of the branch, subprocess, Fork is the creation of a child process, the parent process and the subprocess from the position of the fork to continue the execution, the difference is that the parent process, the resulting fork return value is the child process number, And the child process gets 0.

Note: PHP has a pcntl_fork function to implement multiple processes, but to load pcntl expansion, and only under Linux to compile this expansion.

1. First in the Ubuntu compiler pcntl.so, my Ubuntu can not find the PCNTL package, so create a folder to download the entire PHP package, found inside the PCNTL package to run the following command, the code is as follows:

Copy 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 plug-in module of the compilation environment

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

Cases:

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, the code is as follows:

Copy Code code 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 ' returns 1 ' when creating a child process failure;
Exit (-1);
}
else if ($pid) {
The parent process will get the child process number, so this is the logic that the parent process executes
Pcntl_wait ($status, Wnohang);
}
else{
Child process processing Logic
Sleep (5);
Pcntl_exec ('/usr/bin/php ', $cmd);
Exit (0);
}
}

example, examples of multiple pictures synchronized download, the code is as follows:
Copy Code code as follows:
#!/usr/bin/php
<?php
page addresses that need to be crawled
$url = ' http://www.jb51.net ';
$content = file_get_contents ($url);
Preg_match_all ('/echo "discovered". Count ($matches). Picture n ";

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

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

}
echo "Download complete n";

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

echo "When:" ($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);
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 help you with your PHP program design.

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.