【发布时间】:2013-12-25 18:25:01
【问题描述】:
下面是一些功能齐全的代码。
我计划通过命令行执行此代码,但我希望它在 60 秒后结束。
有谁知道最好的解决方法?
提前致谢。
import time
class listener(StreamListener):
def on_data(self, data):
try:
print data
saveFile = open('twitDB.csv','a')
saveFile.write(data)
saveFile.write('\n')
saveFile.close()
return True
except BaseException, e:
print 'failed ondata,' ,str(e)
time.sleep(5)
def on_error(self, status):
print status
【问题讨论】:
-
记住开始的时间,在
on_data查询时间,过了一分钟就退出? -
开头的'import time'是在记时间吗?我也找不到查询时间的代码...
-
这只是一个类定义。它不会“从命令行运行”。你的意思是来自python解释器?
-
不要抓到
BaseException,除非你以后重新加注。为什么要忽略SystemExit或KeyboardInterrupt? -
基本上我希望代码每小时运行一分钟,我想我会先在 60 秒后取消代码,然后查看使用命令行,以便代码每小时运行一次。这不正确吗?
标签: python