def example()
a = 1
b = 2
try:
c = a + b
except Exception as error:
logwrite.write(str(error))
logwrite.close()
while True:
try:
example()
except Exception as error:
logwrite.write(str(error))
logwrite.close()
time.sleep(600)
为什么程序出现了在执行c = a + b时卡住不动了呀?日志也只记录到开始执行 example(),后面就没了
不是每次都卡住,能正常执行通,但是上次挂着程序后卡住了。
应该是你延时太长了等不到结果。sleep以秒为单位,600秒过长,建议缩短一下改为几秒
还有提个建议,example里已经有了try,在循环时就不需要了,可以省略
若要执行下一次循环,可以用continue语句
time.sleep()函数的参数设置太大了 单位是秒哦
#!/usr/bin/python
# -*- coding: UTF-8 -*-
"""
@author: Roc-xb
"""
import time
from threading import Thread
def example():
a = 1
b = 2
try:
c = a + b
print(c)
except Exception as error:
print(error)
if __name__ == '__main__':
while True:
try:
Thread(target=example).start()
except Exception as error:
print(error)
time.sleep(600)
如果logwrite是一个文件,那关闭后当然不能再打开了。
example() 方法在try..except后再加个finally 语句 pass 呢?
如果需要间隔10分钟运行的话,你有没有等待10分钟看结果呢?
每次close之后下次写的时候提前打开log文件了么,没打开
题目里抽象了example内部过程,也可能是example内部死循环了