c – C libcurl,限制下载速度 – cpu使用率达到100%

前端之家收集整理的这篇文章主要介绍了c – C libcurl,限制下载速度 – cpu使用率达到100%前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有CURLOPT_MAX_RECV_SPEED_LARGE api man page的问题.

我想设置最大下载速度,所以我补充说:

curl_off_t max_speed = 1000*25;
curl_easy_setopt(curl_handle,CURLOPT_MAX_RECV_SPEED_LARGE,max_speed);

但问题是当它达到极限(25kB / s)或我设置的时候,cpu使用率达到100%

我用错了方法吗?我想,因为我没有在任何地方找到这个问题.
我试图将这两行添加到原始示例代码中,但它也很疯狂.

我正在使用Windows和libcurl v 7.29

谢谢

可能的解决方案是编写自己的睡眠进度函数,但是……

这是原始代码“getinmemory.c”,增加了我的2行:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <curl/curl.h>

struct MemoryStruct {
  char *memory;
  size_t size;
};


static size_t WriteMemoryCallback(void *contents,size_t size,size_t nmemb,void     *userp)
{
  size_t realsize = size * nmemb;
  struct MemoryStruct *mem = (struct MemoryStruct *)userp;

  mem->memory =(char*) realloc(mem->memory,mem->size + realsize + 1);
  if(mem->memory == NULL) {
    /* out of memory! */
    printf("not enough memory (realloc returned NULL)\n");
    return 0;
  }

  memcpy(&(mem->memory[mem->size]),contents,realsize);
  mem->size += realsize;
  mem->memory[mem->size] = 0;

  return realsize;
}


int main(void)
{
  CURL *curl_handle;
  CURLcode res;

  struct MemoryStruct chunk;

  chunk.memory = (char*)malloc(1);  /* will be grown as needed by the realloc     above */
  chunk.size = 0;    /* no data at this point */

  curl_global_init(CURL_GLOBAL_ALL);

  curl_handle = curl_easy_init();

  curl_easy_setopt(curl_handle,CURLOPT_URL,"http://ns223506.ovh.net/rozne/04e4383e647ce87ce42e73fa00bb0058/wallpaper-812988.jpg");   //Random wallpaper
  curl_easy_setopt(curl_handle,CURLOPT_WRITEFUNCTION,WriteMemoryCallback);
  curl_easy_setopt(curl_handle,CURLOPT_WRITEDATA,(void *)&chunk);
  curl_easy_setopt(curl_handle,CURLOPT_USERAGENT,"libcurl-agent/1.0");

  //My added 2 lines
  curl_off_t max_speed = 1000*25; // 25kB/s
  curl_easy_setopt(curl_handle,max_speed);


  res = curl_easy_perform(curl_handle);

  if(res != CURLE_OK) {
    fprintf(stderr,"curl_easy_perform() Failed: %s\n",curl_easy_strerror(res));
  }
  else {
    printf("%lu bytes retrieved\n",(long)chunk.size);
  }

  curl_easy_cleanup(curl_handle);

  if(chunk.memory)
    free(chunk.memory);

   curl_global_cleanup();

  return 0;
}

解决方法

好问题解决

它只出现在版本curl-7.29.0-devel-mingw32中!

版本

> curl-7.26.0-devel-mingw32
> curl-7.31.0-devel-mingw32

没关系

之前我测试过另一个版本,但我没有将dll复制到正确的文件夹中,我没有使用curl_version()直接在程序中检查;对不起:(

原文链接:https://www.f2er.com/c/118688.html

猜你在找的C&C++相关文章