微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在迭代器未完全耗尽时获取 StopIteration 异常

如何解决在迭代器未完全耗尽时获取 StopIteration 异常

我只是对奇怪的行为感到好奇,因为它似乎还运行良好。一般来说,我的工作很简单:

 def weather_history_obtain_job(rows=itertuples):
        
    for key in mails.api_keys:
        for _ in range(490):
            try:
                row = next(rows)
            except stopiteration:
                print('The end of active Tradepoint dates is reached!')
                return

            weather_frame = get_tp_weather_history(row,key)
            weather_frame[_columns].to_csv('weather_history.csv',mode='a',header=None)

同名python库调度:

itertuples = dates_frame.itertuples()
schedule.every().day.at("07:30").do(weather_history_obtain_job)

while True:
    schedule.run_pending()
    tm.sleep(15)

所以我得到的是几个异常块打印,每天执行一个

The end of active Tradepoint dates is reached!
The end of active Tradepoint dates is reached!

我的问题是如何执行到异常块,而很明显,迭代器还没有到达终点,继续执行,就像我预期的那样需要两天多的时间才能完成任务?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。