简体   繁体   中英

specify stream run-time (python)?

I have a twitter stream (via Twython) running. Rather than simply rely upon keyboard interrupt, I would like to disconnect() after specified time in seconds. Being new to programming, I am unclear on where the statements found in question: Running a python script for a user-specified amount of time? should be placed (given that I am using a streamer class). Currently, my code stands as follows:

class MyStreamer(TwythonStreamer):    #import from twython
    def on_success(self, data):
        json.dump(data,outfilePrep.outfile,sort_keys=False,indent=1)

    def on_error(self, status_code, data):
        print(status_code)
        outfilePrep.outfile.close()

stream = MyStreamer(<credentials>)
stream.statuses.filter(track=<stuff>,locations=<otherstuff>)

Thanks for your help, and thanks for being easy on a newbie.

There's a "disconnect" method on TwitterStreamer that you'll have to call to stop listenning when your run time is at its end. To check for the running time, you may check it in every callbacks, and you may also register on_timeout to check it in case no tweets are coming.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM