简体   繁体   中英

Multiple inheritance issue, add threading.Thread to the class

First of all let me explain what I want to do :

Basic overview : Get data from twitter in one class and print it in another class.
Little Deeper: I want CustomStreamListener to be able to run as a separate thread. And Every single status, that I'm getting in on_status method put in a queue. Than Twitter() class will pull the queue out and print it.

Now what I don't understand :

  • What I should put in run() function? (Should it just call on_status() ?)
  • In case If everything is good with CustomStreamListener how should I call it?? ( In order for me to call this class without Thread I should do something like this:

      l = CustomStreamListener() stream = tweepy.streaming.Stream(self.auth,l) stream.filter(follow=None, track=hashtag) 
  • Now If I want to call threading.Thread I have to call it with start() and than somewhere after pull the queue out. So this part is little confusing for me.

I'm also open to any other way to get similar result. Thank you.

    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()
stream.filter(follow=None, track=hashtag)

You want to make this function call from the run method, in order to run the event loop in the other thread.

Perhaps easier would be to construct a threading.Thread directly in Twitter.start . Then the listener object does not need to know the parameters you want to pass to filter . In this appoach, CustomStreamListener would not subclass 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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM