Apoorv Ashutosh
Apoorv Ashutosh

Reputation: 4024

How to have a function exit after 1 hour in Python

I am making a simple app to stream twitter's public timeline, and I want the streaming to stop automatically after an hour, and I have no idea how to do this. I read the datetime and the timeit docs, but cannot understand them. Here is my code, and it is streaming the timeline I want perfectly, but indefinitely.

from twython import TwythonStreamer
import json
import os
import datetime
from datetime import *
APP_KEY = 'XX'
APP_SECRET = 'XX'
OAUTH_TOKEN = 'XX'
OAUTH_TOKEN_SECRET = 'XX'
class MyStreamer(TwythonStreamer):

    def on_success(self, data):
        print data['text']
        with open('scratch1.json', 'ab') as outfile:
            json.dump(data, outfile, indent = 4)
        with open('scratch2.json', 'ab') as xoutfile:
            json.dump(data, xoutfile, indent = 4)
        return


    def on_error(self, status_code, data):
        print status_code
        return True # Don't kill the stream

    def on_timeout(self):
        print >> sys.stderr, 'Timeout...'
        return True # Don't kill the stream

stream = MyStreamer(APP_KEY, APP_SECRET,
                    OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
stream.statuses.filter(follow = [95995660, 8820362])

Can anyone help me?

Upvotes: 1

Views: 2787

Answers (3)

ZacharyST
ZacharyST

Reputation: 676

I could not Apoorv's code to replicate with pztrick's modification. Writing:

class MyStreamer(TwythonStreamer):  
   def __init__(self):  
        self.stop_time = dt.datetime.now() + dt.timedelta(minutes=1)

would generate this error message:

TypeError: __init__() takes 1 positional argument but 5 were given

The following did not work:

class MyStreamer(twy.TwythonStreamer):

def __init__(self):
    self.stop_time = dt.datetime.now() + dt.timedelta(minutes=1)
    self.app_key = APP_KEY
    self.app_secret = APP_SECRET
    self.oauth_token = OAUTH_TOKEN
    self.oauth_token_secret = OAUTH_TOKEN_SECRET

What did work, however, was to just define stop_time without init. My final solution looks like:

class MyStreamer(twy.TwythonStreamer):

    stop_time = dt.datetime.now() + dt.timedelta(minutes=1)

    def on_success(self, data):
        if dt.datetime.now() > self.stop_time:
            raise Exception('Time expired')

        fileName = self.fileDirectory + 'Tweets_' + dt.datetime.now().strftime("%Y_%m_%d_%H") + '.txt'  # File name includes date out to hour.
        open(fileName, 'a').write(json.dumps(data) + '\n')

I am new to classes so do not understand why that works, but I am happy that it does.

Upvotes: 2

Zuko
Zuko

Reputation: 2914

i recommend the datetime.datetime.now() module
datetime.datetime.now() + datetime.deltatime(seconds=3600) as your one hour stopage time.

Upvotes: 0

pztrick
pztrick

Reputation: 3831

Use the datetime.datetime.now() method to get a current datetime object, then use the timedelta class to add an hour to it.

import datetime
stop_time = datetime.datetime.now() + datetime.timedelta(hours=1)

# ...

# in relevant function ...
if datetime.datetime.now() > stop_time:
    stop_streaming()

I'm not familiar with you TwythonStreamer class, but possibly something like this:

class MyStreamer(TwythonStreamer):

    # the init function is called when you create instance of class
    def __init__(self):
        self.stop_time = datetime.datetime.now() + datetime.timedelta(hours=1)

    # ...

    def on_success(self, data):
        if datetime.datetime.now() > self.stop_time:
            raise Exception("Time expired")

        # ...

Upvotes: 3

Related Questions