PHP cURL multi_exec 请求之间的延迟
如果我运行标准 cURL_multi_exec 函数(下面的示例),我会立即获得请求的所有 cURL 句柄。我想在每个请求之间设置 100 毫秒的延迟,有办法做到这一点吗? (在Google和StackOverflow搜索中找不到任何内容)
我在curl_multi_exec()之前尝试过usleep(),这会减慢脚本速度,但不会推迟每个请求。
// array of curl handles & results
$curlies = array();
$result = array();
$mh = curl_multi_init();
// setup curl requests
for ($id = 0; $id <= 10; $id += 1) {
$curlies[$id] = curl_init();
curl_setopt($curlies[$id], CURLOPT_URL, "http://google.com");
curl_setopt($curlies[$id], CURLOPT_HEADER, 0);
curl_setopt($curlies[$id], CURLOPT_RETURNTRANSFER, 1);
curl_multi_add_handle($mh, $curlies[$id]);
}
// execute the handles
$running = null;
do {
curl_multi_exec($mh, $running);
} while($running > 0);
// get content and remove handles
foreach($curlies as $id => $c) {
$result[$id] = curl_multi_getcontent($c);
curl_multi_remove_handle($mh, $c);
}
// all done
curl_multi_close($mh);
我一整天都在研究这个问题,任何帮助将不胜感激!谢谢。
编辑:还有其他非 cUrl 方法吗?这也可以回答我的问题。
If I run a standard cURL_multi_exec function (example below), I get all cURL handles requested at once. I would like to put a delay of 100ms between each request, is there a way to do that? (nothing found on Google & StackOverflow search)
I've tried usleep() before curl_multi_exec() which slows down the script but does not postpone each request.
// array of curl handles & results
$curlies = array();
$result = array();
$mh = curl_multi_init();
// setup curl requests
for ($id = 0; $id <= 10; $id += 1) {
$curlies[$id] = curl_init();
curl_setopt($curlies[$id], CURLOPT_URL, "http://google.com");
curl_setopt($curlies[$id], CURLOPT_HEADER, 0);
curl_setopt($curlies[$id], CURLOPT_RETURNTRANSFER, 1);
curl_multi_add_handle($mh, $curlies[$id]);
}
// execute the handles
$running = null;
do {
curl_multi_exec($mh, $running);
} while($running > 0);
// get content and remove handles
foreach($curlies as $id => $c) {
$result[$id] = curl_multi_getcontent($c);
curl_multi_remove_handle($mh, $c);
}
// all done
curl_multi_close($mh);
I'm working on this all day, any help would be greatly appreciated! Thank you.
EDIT: Any other non-cUrl method? That would also answer my question.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
是的,这是可能的。如果您使用 ParallelCurl 库,您可以使用
usleep()
,因为您可以发出单独的请求添加到下载 队列。Yes, this is possible. If you use the ParallelCurl library, you can much more easily add your 100ms delay with
usleep()
, as you can make a separate request to be added to the download queue.别以为你可以。如果您从 cli 运行此命令,您可以将脚本分叉到10 个进程,然后从每个进程发出定期的卷曲请求。这将使您能够对时间进行细粒度的控制。
Don't think you can. If you run this from the cli, you could instead fork your script into 10 processes and then fire regular curl requests from each. That would allow you fine grained control over the timing.
PHP 不是这个问题的解决方案。分叉脚本也无济于事。一开始是的,但是一旦你有更多的网站,你需要像这样抓取,你会发现自己的服务器非常非常红。就成本和脚本稳定性而言,您应该重新考虑使用其他想法。
您可以使用 Python 轻松地做到这一点,并且在对 API 端点进行非阻塞实时调用的情况下,您应该使用 Socket.IO + Node.JS 或只是 Node.JS 之类的东西,或者好吧,呵呵...哈哈,
如果您这样做没有时间也不会使用这样的东西:
http://framework.zend.com/manual/en/zendx.console.process.unix.overview.html
这实际上完全取决于您想要实现的目标。
PHP is not solution for that. Forking the script won't help too. In the beginnings yes but once you have a little bit more websites you need to grab like that you will find yourself as your sever very, very red. In terms of costs and in terms of script stability you should reconsider using some other idea.
You can do that with Python easily and in case of non-blocking real time calls to API endpoints you should use stuff like Socket.IO + Node.JS or just Node.JS or well, huh... lol
In case that you do not have time nor will you can use stuff like this:
http://framework.zend.com/manual/en/zendx.console.process.unix.overview.html
It actually all depends on what are you trying to achieve.
你可以试试这个:
在数据库中存储时间戳,添加一个句柄并调用
curl_multi_exec
。使用
CURLOPT_PROGRESSFUNCTION
检查计时并在需要时添加更多句柄。此处 Daniel Stenberg(cURL 和 libcurl 的作者)表示,在执行
curl_multi_exec
。You can try this:
Store a timestamp in the DB, add one handle and call to
curl_multi_exec
.Use
CURLOPT_PROGRESSFUNCTION
to check timings and add more handles when you need it.Here Daniel Stenberg (author of cURL and libcurl) says it's possible to add more handles after executing
curl_multi_exec
.