title: Unable to stop Streaming in tweepy after one minute
body: <p>I am trying to stream twitter data for a period of time of say 5 minutes, using the Stream.filter() method. I am storing the retrieved tweets in a JSON file. The problem is I am unable to stop the filter() method from within the program. I need to stop the execution manually. I tried stopping the data based on system time using the time package. I was able to stop writing tweets to the JSON file but the stream method is still going on, but It was not able to continue to the next line of code.
  I am using IPython notebook to write and execute the code.
  Here's the code:</p>
  
  <pre><code>auth = OAuthHandler(consumer_key, consumer_secret)
  auth.set_access_token(access_token, access_secret)
  api = tweepy.API(auth)
  
  from tweepy import Stream
  from tweepy.streaming import StreamListener
  
  class MyListener(StreamListener):
  
      def __init__(self, start_time, time_limit=60):
          self.time = start_time
          self.limit = time_limit
  
      def on_data(self, data):
          while (time.time() - self.time) &lt; self.limit:
              try:
                  saveFile = open('abcd.json', 'a')
                  saveFile.write(data)
                  saveFile.write('\n')
                  saveFile.close()
                  return True
              except BaseException as e:
                  print 'failed ondata,', str(e)
                  time.sleep(5)
          return True
  
      def on_status(self, status):
          if (time.time() - self.time) &gt;= self.limit:
              print 'time is over'
              return false
  
      def on_error(self, status):
          if (time.time() - self.time) &gt;= self.limit:
              print 'time is over'
              return false
          else:
              print(status)
              return True
  
  start_time = time.time()
  stream_data = Stream(auth, MyListener(start_time,20))
  stream_data.filter(track=['name1','name2',...list ...,'name n'])#list of the strings I want to track
  </code></pre>
  
  <p>These links are similar but I does not answer my question directly</p>
  
  <p><a href="https://stackoverflow.com/questions/28701962/tweepy-stream-data-for-x-minutes">Tweepy: Stream data for X minutes?</a></p>
  
  <p><a href="https://stackoverflow.com/questions/24663403/stopping-tweepy-steam-after-a-duration-parameter-lines-seconds-tweets-etc">Stopping Tweepy steam after a duration parameter (# lines, seconds, #Tweets, etc)</a></p>
  
  <p><a href="https://stackoverflow.com/questions/20863486/tweepy-streaming-stop-collecting-tweets-at-x-amount">Tweepy Streaming - Stop collecting tweets at x amount</a></p>
  
  <p>I used this link as my reference,
  <a href="http://stats.seandolinar.com/collecting-twitter-data-using-a-python-stream-listener/" rel="noreferrer">http://stats.seandolinar.com/collecting-twitter-data-using-a-python-stream-listener/</a></p>