我正在使用pycurl连接到Twitter流API.
此方法效果很好,但有时运行数小时后,它将无限期停止挂起,不会引发任何异常.如何在此脚本中检测/处理挂起?
import pycurl, json
STREAM_URL = "http://stream.twitter.com/1/statuses/filter.json"
USER = "presidentskroob"
PASS = "12345"
def on_receive(data):
print data
conn = pycurl.Curl()
conn.setopt(pycurl.USERPWD, "%s:%s" % (USER, PASS))
conn.setopt(pycurl.URL, STREAM_URL)
conn.setopt(pycurl.WRITEFUNCTION, on_receive)
conn.perform()
解决方法:
从:http://man-wiki.net/index.php/3:curl_easy_setopt
CURLOPT_LOW_SPEED_LIMIT
– Pass a long as parameter. It contains the
transfer speed in bytes per second that the transfer should be below
duringCURLOPT_LOW_SPEED_TIME
seconds for the library to consider it
too slow and abort.
和
CURLOPT_LOW_SPEED_TIME
– Pass a long as parameter. It contains the
time in seconds that the transfer should be below theCURLOPT_LOW_SPEED_LIMIT
for the library to consider it too slow and
abort.
例:
conn.setopt(pycurl.LOW_SPEED_LIMIT, 1)
conn.setopt(pycurl.LOW_SPEED_TIME, 90)