摊档的东西可以预料的,你必须考虑他们(和其他类型的连接错误)在你的Twitter客户端。 TwitterAPI图书馆确实检查摊位。当它们发生时TwitterConnectionError
被抛出。这是一个信号,您只需重试您的请求。你不需要设置一个90秒的定时器。这由TwitterAPI为您完成。为了实现真正健壮的代码,您还应该处理TwitterRequestError
。这里是处理这两个例外的模板,以保持您的代码运行并保持在Twitter的指导原则中:
from TwitterAPI import TwitterAPI
api = TwitterAPI(CONSUMER_KEY,
CONSUMER_SECRET,
ACCESS_TOKEN_KEY,
ACCESS_TOKEN_SECRET)
while True:
try:
iterator = api.request('statuses/filter', {'track':'pizza'}).get_iterator()
for item in iterator:
if 'text' in item:
print(item['text'])
elif 'disconnect' in item:
event = item['disconnect']
if event['code'] in [2,5,6,7]:
# something needs to be fixed before re-connecting
raise Exception(event['reason'])
else:
# temporary interruption, retry request
break
except TwitterRequestError as e:
if e.status_code < 500:
# something needs to be fixed before re-connecting
raise
else:
# temporary interruption, retry request
pass
except TwitterConnectionError:
# temporary interruption, retry request
pass
阅读文档[https://dev.twitter.com/streaming/overview/connecting )。有关于*摊位*的部分。这应该给你关于你正在经历的信息。查找异常将导致多个链接提供有关该异常的信息,从而为发生这种情况提供背景信息。 – idjaw
@idjaw好的,谢谢让我检查他们 –