最近供一个服务用到了curl,发现一件奇怪的事。
curl的超时如果设置<1000ms的话有可能会返回失效甚至崩溃...
到网上查了一下原因:
CURLOPT_TIMEOUT_MS
The maximum number of milliseconds to allow cURL functions to execute. If libcurl is built to use the standard system name resolver, that portion of the connect will still use full-second resolution for timeouts with a minimum timeout allowed of one second.
官方文档上说curl的超时只支持秒级,因为curl底层的select()调用超时位1000ms。
如果要完成毫秒级的超时,可以使用curl_multi_select()。或者修改原代码。可以参见一个同行的连接:
http://hi.baidu.com/tarotme/blog/item/07bdaefb6921c4284f4aeaec.html
curl的超时如果设置<1000ms的话有可能会返回失效甚至崩溃...
到网上查了一下原因:
CURLOPT_TIMEOUT_MS
The maximum number of milliseconds to allow cURL functions to execute. If libcurl is built to use the standard system name resolver, that portion of the connect will still use full-second resolution for timeouts with a minimum timeout allowed of one second.
官方文档上说curl的超时只支持秒级,因为curl底层的select()调用超时位1000ms。
如果要完成毫秒级的超时,可以使用curl_multi_select()。或者修改原代码。可以参见一个同行的连接:
http://hi.baidu.com/tarotme/blog/item/07bdaefb6921c4284f4aeaec.html
本文探讨了curl在毫秒级超时时可能出现的问题,并解释了其原因。官方文档表明,curl的超时机制仅支持秒级,而底层的select()调用超时位1000ms。为了实现毫秒级超时,可以采用curl_multi_select()或修改源代码。文章还提供了一个同行的经验分享链接,帮助读者进一步了解如何解决这一问题。

被折叠的 条评论
为什么被折叠?



