2013-04-04 106 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回調。