Search code examples
pythonmultithreadingpython-3.xtwittertweepy

How to Thread a Tweepy Stream


I am making a program that pulls live tweets using Tweepy, and runs it through a function. I am using a Tweepy Stream to accomplish the task of getting the tweets. However, what I wish is that while the function is running, Tweepy can still pull these live tweets. I believe I need to thread the Tweepy event listener but I'm not entirely sure how to do this.

def doesSomething(s):
    #function here

class listener(tweepy.StreamListener):
    def on_status(self, status):
        doesSomething(status)

stream_listener = listener()
tweepy.Stream(auth = api.auth, listener=stream_listener)
stream.filter(track=["Example"])

Solution

  • This is an example you can adapt easily... The code will create a thread that updates a counter in the background while a loop will continuously print the value of the counter. The 'stop' variable takes care of quitting the thread when the main process is killed or exit...

    import threading
    import time
    from sys import stdout
    
    # Only data wihtin a class are actually shared by the threads
    class Counter(object):
        counter = 0
        stop = False
    
    # Function that will be executed in parallel with the rest of the code
    def function_1():
        for i in range(10):
            if c.stop: return # With more will exit faster
            time.sleep(2)
            c.counter += 1
            if c.stop: return
    
    # Create a class instance
    c = Counter()
    
    # Thread function_1
    d = threading.Thread(target=function_1)
    d.start()
    
    # Exit the thread properly before exiting...
    try:
        for j in range(100):
            stdout.write('\r{:}'.format(c.counter))
            stdout.flush()
            time.sleep(1)
    except:
        c.stop = True