curl使用curl_easy_perform导致线程或者进程卡死解决办法

来源:互联网 发布:美丽说聊天软件 编辑:程序博客网 时间:2024/05/11 02:59

描述:curl提供的curl_easy_perform调用方式是阻塞的,如果没有收到回复,则会导致线程或者进程一直阻塞,除非外界干预。

解决办法:curl提供了CURLOPT_LOW_SPEED_LIMIT  CURLOPT_LOW_SPEED_TIME  option,主要思想为:如果在指定时间传输速率超过设置的最低值,

                    则会自动断开该链接。

举例:

                  curl_easy_setopt(curl, CURLOPT_LOW_SPEED_LIMIT , 50);

          curl_easy_setopt(curl, CURLOPT_LOW_SPEED_TIME , 5);

                  res = curl_easy_perform(curl);


官方文档解释:

CURLOPT_LOW_SPEED_TIME - set low speed limit time period


CURLOPT_LOW_SPEED_LIMIT - set low speed limit in bytes per second


CURLOPT_LOW_SPEED_TIME  Pass a long as parameter. It contains the time in number seconds that the transfer speed should be below theCURLOPT_LOW_SPEED_LIMIT for the library to consider it too slow and abort.



具体可以参考curl官方文档:

 https://curl.haxx.se/libcurl/c/CURLOPT_TIMEOUT.html

https://curl.haxx.se/libcurl/c/CURLOPT_TIMEOUT.html


对于curl没有特别深入的学习,如果有更好的办法或者上面描述有误,请留言指正,谢谢。

0 0
原创粉丝点击