2013-04-04 105 views
1

我已经在Tweepy中使用了一个用于Python的Twitter API库。虽然我尝试了大多数我在网上找到的方法,但他们未能关闭连接或停止流。有没有办法做到这一点?如何关闭推特流

在我的功能

setTerms = s.split(',') 
streaming_api = tweepy.Stream(auth=auth, listener=StreamListener(), timeout=60) 
    if (s == '0'): 
     streaming_api.disconnect() 
     raise web.seeother('/dc') 
     print "Failed to see this" 
    try: 
     twt = streaming_api.filter(track=setTerms) 
    except: 
     streaming_api.disconnect() 
     #also cannot see this 
    raise web.seeother('/stream') 

这里是流监听器类

class StreamListener(tweepy.StreamListener): 
     def on_status(self, status): 
      try: 
       printer(status.text, status.created_at) 
      except Exception, e: 
       pass 
     def on_error(self, status_code): 
      print >> sys.stderr, 'Encountered error with status code:', status_code 
      return True 
     def on_timeout(self): 
      print >> sys.stderr, 'Timeout...' 
      return True 

回答

0

第一次调用stream.disconnect()(内if (s == '0'):),你有没有叫filter呢,所以流将永远不会被连接。假设您使用最新版本的tweepy,则其余代码应该是正确的。请注意,except块几乎不会被调用,因为流正在运行时发生的任何错误都将传递给on_error回调。