how to properly close a tweepy stream

Wilco picture Wilco · Dec 31, 2012 · Viewed 8.8k times · Source

I'm trying to figure out how to properly close an asynchronous tweepy stream.

The tweepy streaming module can be found here.

I start the stream like this:

stream = Stream(auth, listener)
stream.filter(track=['keyword'], async=True)

When closing the application, I try to close the stream as simple as:

stream.disconnect()

This method seems to work as intended but it seems to have one problem: the stream thread is still in the middle of the loop (waiting/handling tweets) and is not killed until the next loop, so when the stream receives a tweet even after the app has closed, it still tries to call the listener object (this can be seen with a simple print syntax on the listener object). I'm not sure if this is a bad thing or if it can simply be ignored.

I have 2 questions:

  1. Is this the best way to close the stream or should I take a different approach?
  2. Shouldn't the async thread be created as a daemon thread?

Answer

Burkay picture Burkay · Dec 30, 2015

I had the same problem. I fixed it with restarting the script. Tweepy Stream doesn't stop until the next incoming tweet.

Example:

import sys
import os

python=sys.executable

time.sleep(10)

print "restart"
os.execl(python,python,*sys.argv)

I didn't find another solution.