PHP multi-process, in test

Source: Internet
Author: User

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

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.