for ($i = 0; $i < $i + +)
{
$iPid = Pcntl_fork ();
if ($iPid = =-1)
{
Die (' can\ ' is forked. ');
}
if ($iPid)
{
# Master Process Logic
echo $i. " \ r \ n ";
Exit ();
}
Else
{
# Child Process Logic
$iPid = Posix_getpid (); # Gets the ID of the child process
$rlt =system ('/usr/bin/php-f/home/wwwroot/seo/test_shell.php & '. $i);
Print_r ($RLT);
Exit ();
}
}
Exit ();
Pclose (Popen ("/home/xinchen/backend.php &", ' R ')); Multithreading
Pclose (Popen ("/usr/local/php/bin/php/htdocs/www/site/task.php &", "R"));
Curl_multi example of implementing a multithreaded Crawler
$MH = Curl_multi_init (); Returns a new Curl batch handle
for ($i =; $i < $max _size; $i + +)
{
$ch = Curl_init (); Initializing a single Curl session
curl_setopt ($ch, Curlopt_header,);
curl_setopt ($ch, Curlopt_url, ' http://www.zhihu.com/people/'. $user _list[$i]. '/about ');
curl_setopt ($ch, Curlopt_cookie, self:: $user _cookie);
curl_setopt ($ch, Curlopt_useragent, ' mozilla/. (Windows NT.; WOW) applewebkit/. (khtml, like Gecko) chrome/... safari/. ');
curl_setopt ($ch, Curlopt_returntransfer, true);
curl_setopt ($ch, curlopt_followlocation,);
$requestMap [$i] = $ch;
Curl_multi_add_handle ($MH, $ch); To add a separate curl handle to a curl batch session
}
$user _arr = Array ();
do {
Child connection that runs the current CURL handle
while ($cme = Curl_multi_exec ($MH, $active)) = = Curlm_call_multi_perform);
if ($cme! = CURLM_OK) {break;}
Gets the related transport information for the currently resolved curl
while ($done = Curl_multi_info_read ($MH))
{
$info = Curl_getinfo ($done [' handle ']);
$tmp _result = curl_multi_getcontent ($done [' handle ']);
$error = Curl_error ($done [' handle ']);
$user _arr[] = array_values (GetUserInfo ($tmp _result));
Ensure that there are $max_size requests in process at the same time
if ($i < sizeof ($user _list) && isset ($user _list[$i]) && $i < count ($user _list))
{
$ch = Curl_init ();
curl_setopt ($ch, Curlopt_header,);
curl_setopt ($ch, Curlopt_url, ' http://www.zhihu.com/people/'. $user _list[$i]. '/about ');
curl_setopt ($ch, Curlopt_cookie, self:: $user _cookie);
curl_setopt ($ch, Curlopt_useragent, ' mozilla/. (Windows NT.; WOW) applewebkit/. (khtml, like Gecko) chrome/... safari/. ');
curl_setopt ($ch, Curlopt_returntransfer, true);
curl_setopt ($ch, curlopt_followlocation,);
$requestMap [$i] = $ch;
Curl_multi_add_handle ($MH, $ch);
$i + +;
}
Curl_multi_remove_handle ($MH, $done [' handle ']);
}
if ($active)
Curl_multi_select ($MH,);
} while ($active);
Curl_multi_close ($MH);
return $user _arr;
PHP multi-process, in test