php 多进程,测试中

Posted Microtiger

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 多进程,测试中相关的知识,希望对你有一定的参考价值。

for($i = 0; $i < 10; $i++)
{
$iPid = pcntl_fork();
if($iPid == -1)
{
die(‘can\‘t be forked.‘);
}

if($iPid)
{
# 主进程逻辑
echo $i."\r\n";
// exit();
}
else
{
# 子进程逻辑
$iPid = posix_getpid(); # 获取子进程的ID
$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‘)); // 多线程
pclose(popen("/usr/local/php/bin/php /htdocs/www/site/task.php &", "r"));
//curl_multi实现多线程爬虫的示例

$mh = curl_multi_init(); //返回一个新cURL批处理句柄
for ($i = ; $i < $max_size; $i++)
{
$ch = curl_init(); //初始化单个cURL会话
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); //向curl批处理会话中添加单独的curl句柄
}
$user_arr = array();
do {
//运行当前 cURL 句柄的子连接
while (($cme = curl_multi_exec($mh, $active)) == CURLM_CALL_MULTI_PERFORM);
if ($cme != CURLM_OK) {break;}
//获取当前解析的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));
//保证同时有$max_size个请求在处理
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 多进程,测试中的主要内容,如果未能解决你的问题,请参考以下文章

python中多线程,多进程,队列笔记

python中多进程

Python中多进程的使用

python中多进程和多线程的区别

python中多进程+协程的使用以及为啥要用它

Java中多线程的使用!!