指定流运行时(python)



我有一个twitter流(通过Twython)运行。而不是简单地依赖于键盘中断,我想在指定的时间(以秒为单位)后断开()。作为编程新手,我不清楚问题中的语句在哪里:在用户指定的时间内运行python脚本?应该放置(鉴于我正在使用streamer类)。目前,我的代码如下:

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>)

谢谢你的帮助,也谢谢你对新手这么宽容。

在TwitterStreamer上有一个"disconnect"方法,当你的运行时间结束时,你必须调用它来停止监听。为了检查运行时间,你可以在每个回调中检查它,你也可以注册on_timeout来检查它,以防没有tweet来。

最新更新