Hello, I'm trying to use libcurl multi interface to fetch several data in parallel. I would expect this to be faster than performing repeated fetches using the easy interface, but for some reason I can't obtain any speed up at all: using the multi interface actually turns out to be MUCH slower than launching several sequential "easy" requests.
This is the code that actually performs the requests:
curlmh_ = curl_multi_init();
int min_req = MIN(n_req_, urls_.size());
CURL** curleh = new CURL*[min_req];
// create min_req easy handles, to be added to a single multi handle
for (int i=0; i<min_req; i++) {
curleh = curl_easy_init();
curl_easy_setopt (curleh, CURLOPT_URL, urls_.c_str());
curl_easy_setopt (curleh, CURLOPT_WRITEFUNCTION, fetcher_thread::write_data);
curl_multi_add_handle (curlmh_, curleh);
}
int active, curl_res, fd_res, max_fd;
fd_set fds, fdsw, fdse;
struct timeval timeout;
timeout.tv_sec = 5;
timeout.tv_usec = 0;
do {
do {
curl_res = curl_multi_perform (curlmh_, &active);
}
while ( curl_res == CURLM_CALL_MULTI_PERFORM );
FD_ZERO (&fds); FD_ZERO (&fdsw); FD_ZERO (&fdse);
max_fd = 0;
fd_res = curl_multi_fdset (curlmh_, &fds, &fdsw, &fdse, &max_fd);
if ( max_fd == -1 ) {
continue;
}
do {
fd_res = select (max_fd+1, &fds, &fdsw, &fdse, (struct timeval*)NULL);
}
while ( fd_res == -1 && errno == EINTR );
msleep(100); // I'd like to do other useful stuff meanwhile...
}
while ( curl_res == CURLM_OK && active > 0 );
// operations finished, doing the cleanup
for (int i=0; i<min_req; i++) {
curl_multi_remove_handle (curlmh_, curleh);
curl_easy_cleanup (curleh);
}
curl_multi_cleanup(curlmh_);
I am using libcurl version 7.20.0-r2 under Gentoo Linux.
Any help/suggestion/idea would be really appreciated.
Thank you