This article mainly introduces how to use pcntl_fork to download images from multiple processes in PHP, analyzes in detail the principles and usage of pcntl_fork, and how to use pcntl_fork to download images from multiple processes, it is very practical. if you need it, you can refer to the following example to describe how PHP uses pcntl_fork to download images through multiple processes. Share it with you for your reference. The specific analysis is as follows:
PHP pcntl_fork-generate a branch at the current position of the current process. sub-process, the difference is that during the execution of the parent process, the fork returned value is the sub-process number, and the sub-process gets 0.
Note: PHP has a pcntl_fork function that can implement multiple processes. however, to load the pcntl extension, this extension can be compiled only in linux.
1. compile pcntl in ubuntu. so, my ubuntu cannot find the pcntl package, so I created a folder and downloaded the entire PHP package, and found the pcntl package to run the following command:
The code is as follows:
# Mkdir php
# Cd php
# Apt-get source php5
# Cd php5-(WHATEVER_RELEASE)/ext/pcntl
# Phpize
#./Configure (note 1)
# Make
# Make install phpize command is used to prepare the compiling environment of the PHP plug-in module
Extname will be created after successful installation. so and placed in the PHP plug-in module directory (pre-stored in/usr/lib/php/modules/), you need to adjust php. ini, add extension = extname. this plug-in module can be used only after this line of so.
Example:
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:
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' if the sub-process fails to be created ';
Exit (-1 );
}
Else if ($ pid ){
// The parent process will get the sub-process number, so here is the logic of the parent process execution
Pcntl_wait ($ status, WNOHANG );
}
Else {
// Sub-process processing logic
Sleep (5 );
Pcntl_exec ('/usr/bin/php', $ cmd );
Exit (0 );
}
}
For example, the sample code is as follows:
The code is as follows:
#! /Usr/bin/php
<? Php
// The URL of the webpage to be crawled
$ Url = 'http: // www.jb51.net ';
$ Content = file_get_contents ($ url );
Preg_match_all ('/echo "found". count ($ matches). "n images ";
List ($ sm, $ ss) = explode ("", microtime ());
Foreach ($ matches as $ k => $ val)
{
$ Pid [$ k] = pcntl_fork ();
If (! $ Pid [$ k])
{
Download ($ url, $ val );
// The sub-process must exit. Otherwise, the multi-process will be recursive. the parent process must not exit. Otherwise, the multi-process will be terminated.
Exit (0 );
}
If ($ pid [$ k])
{
// Pcntl_waitpid ($ pid [$ k], $ status, WUNTRACED );
}
}
Echo "download completed n ";
List ($ em, $ es) = explode ("", microtime ());
Echo "time:", ($ es + $ em)-($ ss + $ sm), "n ";
/**
* Capture webpage 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 PHP programming.