1

The below is an extract from my code from a previous question. The aim of this python code is to create a crawler for Twitter where by I plan to run the code every hour for 60 seconds. I am going to do this over 24 hours for 7 days to look at the trends within Tweets.

Currently this code is running until it is manually ended

import time
import threading

class listener(StreamListener):
    def on_data(self, data):
        try:
            print data
            saveFile = open('twitDB.csv','a')
            saveFile.write(data)
            saveFile.write('\n')
            saveFile.close()
            return True
        except BaseException, e:
            print 'failed ondata,' ,str(e)
            time.sleep(5)

            def on_error(self, status):
                print status


time.sleep(5)  
Kara
  • 6,115
  • 16
  • 50
  • 57
user3102640
  • 39
  • 1
  • 1
  • 5
  • So what is the problem, disregarding the contents of the ```listener``` class, what you have would do what you want if you repalces the ```time.sleep(60)``` with ```time.sleep(5)``` –  Dec 26 '13 at 15:43
  • You say “extract” from the code of the previous question when it’s actually mostly unchanged. The question does not seem any different at all—and actually there isn’t a question in what you have written. The previous question gives you enough ideas to work with, so please use those answers to solve it. – poke Dec 26 '13 at 16:14
  • @poke, you are saying right.I just checked that the guy has asked almost the same question again. – darxtrix Dec 26 '13 at 16:50

1 Answers1

1

Put the following code in a running thread other than in which your main code is executing and have a try:

import sys,time
now=time.time()
while True:
    end=time.time()
    total=round(end-now)
    if total==5:
        sys.exit(0)
darxtrix
  • 2,032
  • 2
  • 23
  • 30