pycurl nosignal和timeout<=999ms

2024-06-25 05:50:14 发布

您现在位置:Python中文网/ 问答频道 /正文

import pycurl

pycurl.version
# libcurl/7.29.0 GnuTLS/2.12.23 zlib/1.2.7 libidn/1.25 librtmp/2.3

c = pycurl.Curl()
c.setopt(pycurl.TIMEOUT_MS, 1000)
c.setopt(pycurl.URL, 'http://example.com/')
c.perform()
# ok

c = pycurl.Curl()
c.setopt(pycurl.URL, 'http://example.com/')
c.setopt(pycurl.TIMEOUT_MS, 999)
c.perform()
# pycurl.error: (28, '')

c = pycurl.Curl()
c.setopt(pycurl.URL, 'http://example.com/')
c.setopt(pycurl.TIMEOUT_MS, 999)
c.setopt(pycurl.NOSIGNAL, 1)
c.perform()
# ok again

有人能解释一下为什么不到1秒的超时失败了,nosignal能让它重新工作吗?在


Tags: importcomhttpurlversionexampletimeoutok
2条回答

我认为这是因为CURL所做的一些库调用(例如DNS查找)不支持超时。因此,中断它们的唯一方法就是设置一个信号。在

根据libcurl APItimeout_ms<;1000在使用标准名称解析程序时是不允许的。在

CURLOPT_TIMEOUT_MS

An alternative to CURLOPT_TIMEOUT but takes number of milliseconds instead. If libcurl is built to use the standard system name resolver, that portion of the transfer will still use full-second resolution for timeouts with a minimum timeout allowed of one second.

相关问题 更多 >