简体   繁体   English

指定流运行时(python)?

[英]specify stream run-time (python)?

I have a twitter stream (via Twython) running. 我正在运行一个Twitter流(通过Twython)。 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? 作为编程新手,我不清楚在哪里找到这些语句: 在用户指定的时间内运行python脚本? 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. TwitterStreamer上有一个“断开连接”方法 ,您必须在运行时间结束时调用该方法以停止侦听。 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. 要检查运行时间,您可以在每个回调中对其进行检查,并且还可以注册on_timeout进行检查,以防万一没有任何鸣叫出现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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