Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
指定流运行时(python)?_Python_Multithreading_Time_Stream_Control Flow - Fatal编程技术网

指定流运行时(python)?

指定流运行时(python)?,python,multithreading,time,stream,control-flow,Python,Multithreading,Time,Stream,Control Flow,我有一条推特流(通过Twython)在运行。我希望在指定的时间(以秒为单位)后断开()连接,而不是简单地依赖键盘中断。 作为编程新手,我不清楚有问题的语句应该放在哪里(因为我使用的是streamer类)。 目前,我的代码如下: class MyStreamer(TwythonStreamer): #import from twython def on_success(self, data): json.dump(data,outfilePrep.outfile,so

我有一条推特流(通过Twython)在运行。我希望在指定的时间(以秒为单位)后断开()连接,而不是简单地依赖键盘中断。 作为编程新手,我不清楚有问题的语句应该放在哪里(因为我使用的是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>)
classmystreamer(TwythonStreamer):#从twython导入
def on_成功(自我、数据):
dump(数据,outfilePrep.outfile,排序键=False,缩进=1)
def on_错误(自身、状态代码、数据):
打印(状态代码)
outfilePrep.outfile.close()
stream=MyStreamer()
stream.statuses.filter(轨迹=,位置=)

谢谢你的帮助,也谢谢你对一个新手的宽容

当您的运行时间结束时,您必须调用一个函数来停止侦听。要检查运行时间,您可以在每次回调中检查它,也可以在超时时注册以检查它,以防没有tweet出现。

我想我的问题更基本:有没有这种断开连接的方法,如何在函数或类中构造流对象的自动停止?