PHP pcntl_fork Multi-process Download Picture instance

Source: Internet
Author: User
Tags foreach explode

Attention:

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

1. First in the Ubuntu compiler pcntl.so, my Ubuntu can not find a pcntl bag, so

Create a folder to download the entire PHP package and find the PCNTL package inside to run the following command

The code is as follows Copy Code

# 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/).
You need to adjust the php.ini and join the extension=extname.so to use this plug-in module.

Cases

void Pcntl_exec (String $path [, array $args [, array $envs]])


pcntl_exec-executing the specified program in the current process space

The code is as follows Copy Code


$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);

}

}


Cases

instance multiple picture sync download

The code is as follows Copy Code


#!/usr/bin/php
<?php
page addresses that need to be crawled
$url = ' http://www.111cn.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 * *

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.