简体   繁体   English

Python如何在停止后重新运行代码

[英]Python how to rerun code after it stops

i am newbie to Programming,i am running this python code in my computer Normally it runs correctly, but sometimes the code stops due to network conditions. 我是编程的新手,我在计算机上运行此python代码。正常情况下,它可以正常运行,但有时由于网络条件而停止运行。 once it stopped i have to run the code manually. 一旦停止,我必须手动运行代码。 Does anyone know how to modify this code to retry if the code fails to run 是否有人知道如何修改此代码以在代码无法运行时重试

This is the code 这是代码

for line in f:
    api.update_status(line)
    time.sleep(1200)

The code in Noel's answer makes it more likely that the program doesn't stop until the loop goes through every item in f . Noel的答案中的代码使程序更有可能直到循环遍历f每个项目才停止。

The problem though is that an exception could occur in the line after except and the program would stop again. 但是,问题在于, except之后的行中可能会发生异常,程序将再次停止。

try this: 尝试这个:

i = 0

while i < len(f):
    try:
        api.update_status(f[i])
        i += 1
    except:
        pass

This way, i only gets incremented if api.update is successful so no items will be skipped and the program wont stop until all f are update d 这样,如果api.update成功, i只会递增,因此不会跳过任何项目,并且直到所有fupdate d为止,程序都不会停止

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

相关问题 如何在“堆栈溢出错误”后自动重新运行 python 代码 - how to rerun the python code automatically after “stack overflow error” 在Python中重新运行代码块 - Rerun code block in Python python如何重新运行程序? - python how to rerun program? 完成后如何自动重新运行python程序? Supervisord? - How to automatically rerun a python program after it finishes? Supervisord? 如何使代码从python的第一行重新运行? - How do you make code rerun from the first line in python? Python 代码在 if 语句后停止运行 - Python code stops running after if statement 10 秒后重新运行 python 脚本 - Rerun python script after 10 seconds 如何查找 pandas 数据帧中的任何值是否重复并在用户确认后重新运行一段代码 - How to find if any values in pandas data frame are duplicated and rerun a piece of code after user confirmation 在Pycharm中,即使在我编辑代码并重新运行算法后,如何保持以前的运行结果? - In Pycharm, how to keep my previous runed results even after I edit the code and rerun the algorithm? 如何在每 5 分钟 using.bat 文件后关闭并重新运行 python exe - How to close and rerun a python exe after every 5min using .bat file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM