简体   繁体   English

cURL,是curl_multi_init多线程还是异步API?

[英]cURL, is curl_multi_init multithreaded or is it an async API?

My question is really simple, is cURL's curl_multi_init actually multi threaded or does it just use an asynchronous API? 我的问题很简单,cURL的curl_multi_init实际上是多线程还是只使用异步API? Thanks! 谢谢!

cURL is a very old library, and truly asynchronous code is a relatively new concept. cURL是一个非常古老的库, 真正的异步代码是一个相对较新的概念。 libcurl was written in C, so every single request is blocking. libcurl是用C语言编写的,因此每个请求都是阻塞的。 Although you can process multiple requests in parallel, this is definitely not asynchronous at all for your program as you have to wait until the longest request is finished: 虽然您可以并行处理多个请求,但这对于您的程序来说绝对不是异步的,因为您必须等到最长的请求完成:

while((curl_multi_exec($master, $running)) == CURLM_CALL_MULTI_PERFORM);

That said, I still believe cURL is the best solution. 也就是说,我仍然相信cURL是最好的解决方案。 Other libraries that implement this functionality often also use curl, they just add callbacks so that data can be processed as soon as each individual request is finished, instead of waiting for them all and then reaching 100% CPU to process everything at the end. 实现此功能的其他库通常也使用curl,它们只是添加回调,以便在每个单独的请求完成后立即处理数据,而不是等待它们全部然后达到100%CPU以在最后处理所有内容。

The problem is that most implementations of curl_multi wait for each set of requests to complete before processing them. 问题是curl_multi的大多数实现在处理它们之前等待每组请求完成。 If there are too many requests to process at once, they usually get broken into groups that are then processed one at a time. 如果一次请求处理的请求太多,它们通常会分组,然后一次处理一个。 The problem with this is that each group has to wait for the slowest request to download. 这个问题是每个组都必须等待最慢的下载请求。 In a group of 100 requests, all it takes is one slow one to delay the processing of 99 others. 在一组100个请求中,只需要一个缓慢的延迟处理99个其他请求。 The larger the number of requests you are dealing with, the more noticeable this latency becomes. 您处理的请求数越多,此延迟就越明显。

The solution is to process each request as soon as it completes. 解决方案是在每个请求完成后立即处理。 This eliminates the wasted CPU cycles from busy waiting. 这消除了繁忙等待时浪费的CPU周期。 I also created a queue of cURL requests to allow for maximum throughput. 我还创建了一个cURL请求队列,以允许最大吞吐量。 Each time a request is completed, add a new one from the queue. 每次请求完成后,从队列中添加一个新请求。 By dynamically adding and removing links, we keep a constant number of links downloading at all times. 通过动态添加和删除链接,我们始终保持一定数量的链接下载。 This gives us a way to throttle the amount of simultaneous requests we are sending. 这为我们提供了一种方法来限制我们发送的同时请求的数量。 The result is a faster and more efficient way of processing large quantities of cURL requests in parallel. 结果是以更快和更有效的方式并行处理大量cURL请求。

RollingCurlX Rolling Curl X is a fork of Rolling Curl wrapper cURL Multi. RollingCurlX Rolling Curl X是Rolling Curl包装器cURL Multi的一个分支。 It aims at making concurrent http requests in PHP as easy as possible. 它旨在使PHP中的并发http请求尽可能简单。
RollingCurl for PHP (not updated in 5 years) RollingCurl for PHP (未在5年内更新)

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

Allows the processing of multiple cURL handles asynchronously. 允许异步处理多个cURL句柄。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM