python-处理pycurl挂在Twitter流API上

我正在使用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
during CURLOPT_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 the
CURLOPT_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)
上一篇:Linux Yum致命Python错误:pycurl:libcurl链接时版本比编译时版本早


下一篇:python – Pip Requirements.txt –global-option导致其他软件包的安装错误. “选项未被识别”