2013-04-25 24 views
0

,首先讓我解釋什麼我想要做的多重繼承問題,添加threading.Thread到類

基本概況: 在一個類從Twitter獲取數據,而在另一個打印類。
小深:我想CustomStreamListener能夠作爲一個單獨的線程運行。而且,我將在on_status方法中獲得的每一個狀態都放入隊列中。比Twitter()類將拉出隊列並打印它。

現在什麼,我不明白

  • 我應該把什麼run()功能? (它應該只是打電話on_status()?)
  • 如果一切順利CustomStreamListener我該怎麼稱呼它? (爲了讓我來調用這個類沒有Thread我應該做這樣的事情:

    l = CustomStreamListener() 
        stream = tweepy.streaming.Stream(self.auth,l) 
        stream.filter(follow=None, track=hashtag) 
    
  • 現在,如果我要打電話threading.Thread我有start()比地方拉隊列出來後叫它所以這部分對我來說有點混亂。

我也開到任何其他的方式來獲得類似的結果。謝謝。

import sys, os, time 
    import tweepy 
    import simplejson as json 
    import threading, Queue 

    queue = Queue.Queue() 

    #SETTINGS : 

    #output = open(os.path.join(os.path.expanduser('~/'), 'sample_file.txt'), 'a') 
    hashtag = ['java'] 

    class CustomStreamListener(tweepy.StreamListener, threading.Thread): 

     def __init__(self): 
      threading.Thread.__init__(self) 
      self.queue = queue 

     def on_status(self, status): 
      try: 
       # If you want to have different data from twitter just change .text to something different from tweet: 
       ''' 
       status.text 
       status.author.screen_name 
       status.created_at 
       status.source 
       status.user.screen_name 
       status.id 
       status.source_url 
       status.place 
       ''' 
       time.sleep(0.15) 
       data = status.text 
       self.queue.put(data) 

       #print json.loads(data) 
       #with open(os.path.join(os.path.expanduser('~/'), 'sample_file.txt'), 'a') as output: 
       # output.write("%s\n "% data) 

      except Exception, e: 
       print >> sys.stderr, 'Encountered Exception:', e 
       pass 

     ''' 
     def on_data(self, data): 
      d = (json.dumps(json.loads(data), indent=4, sort_keys=True)) 
      print d 
      output.write("%s "% d) 
     ''' 

     def on_error(self, status_code): 
      print >> sys.stderr, 'Encountered error with status code:', status_code 
      return True # Don't kill the stream 

     def on_timeout(self): 
      print >> sys.stderr, 'Timeout...' 
      return True # Don't kill the stream 

    class Twitter(): 

     def __init__(self): 

      consumer_key='' 
      consumer_secret='' 
      access_key = '' 
      access_secret = '' 

      self.auth = tweepy.OAuthHandler(consumer_key, consumer_secret) 
      self.auth.set_access_token(access_key, access_secret) 
      self.api = tweepy.API(self.auth) 


     def start(self): 
      l = CustomStreamListener() 
      stream = tweepy.streaming.Stream(self.auth,l) 
      stream.filter(follow=None, track=hashtag) 

    if __name__ == "__main__": 
     Twitter().start() 
+0

「filter」函數是一個事件循環嗎?它基本上是永遠運行嗎? – 2013-04-25 15:43:55

+0

是的,過濾器功能是流的一部分,它將永遠運行。 – Vor 2013-04-25 15:45:50

回答

0
stream.filter(follow=None, track=hashtag) 

您想從run方法中調用此函數,以便在另一個線程中運行事件循環。

也許更容易的是直接在Twitter.start中構建threading.Thread。然後偵聽器對象不需要知道要傳遞給filter的參數。在這個例子中,CustomStreamListener不會繼承threading.Thread

class Twitter(): 
    def start(self): 
     l = CustomStreamListener() 
     stream = tweepy.streaming.Stream(self.auth,l) 
     self.listener_thread = threading.Thread(target=stream.filter, 
               kwargs=dict(follow=None, track=hashtag)) 
     self.listener_thread.start() 
+0

我也在想這個,但是,我會在q查詢中輸入什麼? – Vor 2013-04-25 17:49:08

+0

抱歉,錯誤,''....我會在查詢中輸入什麼內容「' – Vor 2013-04-25 19:00:50

+0

@Vor通過查詢,您的意思是」follow「和」track「參數嗎?見編輯的答案。 – 2013-04-26 05:53:01