我对 tweepy python 库比较陌生。
我想确保我的流 python 脚本始终在远程服务器上运行。因此,如果有人能够分享如何实现这一目标的最佳实践,那就太好了。
现在我正在这样做:
if __name__ == '__main__':
while True:
try:
# create instance of the tweepy tweet stream listener
listener = TweetStreamListener()
# set twitter keys/tokens
auth = OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
# create instance of the tweepy stream
stream = Stream(auth, listener)
stream.userstream()
except Exception as e:
print "Error. Restarting Stream.... Error: "
print e.__doc__
print e.message
time.sleep(5)
我回来了False
对于每种方法:on_error(), on_disconnect(), on_timeout()
。
所以,通过返回False
流停止然后在无限循环中重新连接。
这是我的做法,它已经在两台计算机上运行了近一年,以处理使流停止的错误。
#They don't need to be in the loop.
auth = OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
while True:
listener = TweetStreamListener()
stream = Stream(auth, listener, timeout=60)
try:
stream.userstream()
except Exception, e:
print "Error. Restarting Stream.... Error: "
print e.__doc__
print e.message
为了确保它永远运行,您应该重新定义on_error
方法来处理重新连接尝试之间的时间。你的 5 秒睡眠会阻碍你成功重新连接的机会,因为 Twitter 会发现你尝试这样做的频率太高。但这是另一个问题了。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)