python - Multiple inheritance issue, add threading.Thread to the class -


first of let me explain what want do:

basic overview: data twitter in 1 class , print in class.
little deeper: want customstreamlistener able run separate thread. , every single status, i'm getting in on_status method put in queue. twitter() class pull queue out , print it.

now what don't understand:

  • what should put in run() function? (should call on_status()?)
  • in case if customstreamlistener how should call it?? ( in order me call class without thread should this:

        l = customstreamlistener()     stream = tweepy.streaming.stream(self.auth,l)     stream.filter(follow=none, track=hashtag) 
  • now if want call threading.thread have call start() , somewhere after pull queue out. part little confusing me.

i'm open other way similar result. thank you.

    import sys, os, time     import tweepy     import simplejson 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 want have different data twitter change .text different 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') 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 status code:', status_code             return true # don't kill stream          def on_timeout(self):             print >> sys.stderr, 'timeout...'             return true # don't kill 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 make function call run method, in order run event loop in other thread.

perhaps easier construct threading.thread directly in twitter.start. listener object not need know parameters want pass filter. in appoach, customstreamlistener 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() 

Popular posts from this blog

How to calculate SNR of signals in MATLAB? -

c# - Attempting to upload to FTP: System.Net.WebException: System error -

ios - UISlider customization: how to properly add shadow to custom knob image -