PHP使用CURL的cronjob多处理 [英] PHP multiprocessing with cronjob using CURL

查看:150
本文介绍了PHP使用CURL的cronjob多处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要做的是使用PHP处理数据库中待处理的多个请求。



我目前想做的是:
当我的cronjob运行。我想调用一个文件process_a.php10次,立即,而不用等待它完成处理(process_a.php可能需要几分钟)。



我试图这样使用CURL。但是当我的cronjob调用process_a.php时,它会等待它完成处理并在调用另一个process_a.php文件之前返回。



我甚至尝试将代码放入process_a。 php立即关闭连接,然后在后台继续处理。但是cronjob仍然等待它完成。



我只想让同一个文件一次执行10次,你知道,就好像10个不同的用户请求index.php我的网站的页面...
任何想法!

解决方案

多执行程序应该是一个选项。



http://php.net/manual/en/function.curl-multi-exec.php

 <?php 
//创建多个cURL句柄
$ mh = curl_multi_init();

//创建两个cURL资源
for($ i = 0; $ i <10; $ i ++){
$ ch [$ i] = curl_init
curl_setopt($ ch [$ i],CURLOPT_URL,http://urhost//path/to/process_a.php);
curl_setopt($ ch [$ i],CURLOPT_HEADER,0);
curl_multi_add_handle($ mh,$ ch [$ i]); //添加句柄
}

$ active = null;

//执行句柄
do {
$ mrc = curl_multi_exec($ mh,$ active);
} while($ mrc == CURLM_CALL_MULTI_PERFORM);

while($ active& $ mrc == CURLM_OK){
if(curl_multi_select($ mh)!= -1){
do {
$ mrc = curl_multi_exec($ mh,$ active);
} while($ mrc == CURLM_CALL_MULTI_PERFORM);
}
}

//关闭句柄
for($ i = 0; $ i <10; $ i ++){
curl_multi_remove_handle $ mh,$ ch [$ i]);
}
curl_multi_close($ mh);

?>

我通过调用下面的另一个脚本来测试这个脚本:

 <?php 
print microtime(); //返回当前Unix时间戳微秒
print'< br&
?>

这里是结果,每个句柄在执行时间上相差微秒。



0.27085300 1340214659
0.44853600 1340214659
0.46611800 1340214659
0.48201000 1340214659
0.50209400 1340214659
0.48233900 1340214659
0.52274300 1340214659
0.54757800 1340214659
0.57316900 1340214659
0.59475800 1340214659


What I need to do is process multiple requests that are pending in a database, using PHP.

What I'm currently trying to do is: When my cronjob runs. I want to call a file "process_a.php" 10 times, instantly, without waiting for it to finish processing (process_a.php could take a few minutes).

I tried to do this using CURL. But right when my cronjob calls process_a.php, it waits for it to finish processing and return before calling another process_a.php file.

I even tried putting code in process_a.php to close the connection instantly, then continue processing in the background. but the cronjob still waited for it to finish.

I just want the same file being executed 10 times at once, you know, like if 10 different users were to request the index.php page of my website... Any ideas!?

解决方案

As told by @Brad curl-multi-exec should be an option.

http://php.net/manual/en/function.curl-multi-exec.php

    <?php
//create the multiple cURL handle
$mh = curl_multi_init();

// create both cURL resources
for($i = 0; $i < 10;$i++){
     $ch[$i] = curl_init();
     curl_setopt($ch[$i], CURLOPT_URL, "http://urhost//path/to/process_a.php");
     curl_setopt($ch[$i], CURLOPT_HEADER, 0);
     curl_multi_add_handle($mh,$ch[$i]);//add the handles
}

$active = null;

//execute the handles
do {
    $mrc = curl_multi_exec($mh, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);

while ($active && $mrc == CURLM_OK) {
    if (curl_multi_select($mh) != -1) {
        do {
            $mrc = curl_multi_exec($mh, $active);
        } while ($mrc == CURLM_CALL_MULTI_PERFORM);
    }
}

//close the handles
for($i = 0; $i < 10;$i++){
     curl_multi_remove_handle($mh, $ch[$i]);
}
curl_multi_close($mh);

?>

I tested this script by calling another script below :

<?php 
print microtime();//Return current Unix timestamp with microseconds 
print '<br>';
?>

and here are the results, each handle differing by microseconds in execution time.

0.27085300 1340214659
0.44853600 1340214659
0.46611800 1340214659
0.48201000 1340214659
0.50209400 1340214659
0.48233900 1340214659
0.52274300 1340214659
0.54757800 1340214659
0.57316900 1340214659
0.59475800 1340214659

这篇关于PHP使用CURL的cronjob多处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆