多继承问题,添加threading.Thread到类

时间:2013-04-25 13:11:57

标签: python multithreading twitter multiple-inheritance tweepy

首先让我解释我想做什么

基本概述: 从一个班级的twitter获取数据并将其打印在另一个班级中 Little Deeper:我希望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()

1 个答案:

答案 0 :(得分:0)

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

您希望从run方法调用此函数,以便在另一个线程中运行事件循环。

或许更容易直接在threading.Thread中构建Twitter.start。然后,侦听器对象不需要知道要传递给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()