[英]Python current time doesn't change when the code is running on a loop
我有一个 python 脚本,它每 5 秒运行一次 function。 我注意到当代码循环时,时间 output 没有改变。 只有当我停止代码并再次运行它时它才会改变。 但我代码中的所有其他数据每 5 秒更新一次。 我不知道日期的情况是什么,因为我没有测试过。 因此,当再次调用 function 时,它会显示它首次执行的时间。 谢谢你的时间。
代码
from datetime import datetime
from datetime import date
import time
today = date.today()
now = datetime.now()
def loaddata():
date1 = today.strftime("%d/%m/%Y")
print("d1 =", date1)
current_time = now.strftime("%H:%M:%S")
print("Current Time =", current_time)
schedule.every(5).seconds.do(loaddata)
while 1:
schedule.run_pending()
time.sleep(1)
您必须today
和now
在fuction
中声明:
from datetime import datetime
from datetime import date
import time
def loaddata():
today = date.today()
now = datetime.now()
date1 = today.strftime("%d/%m/%Y")
print("d1 =", date1)
current_time = now.strftime("%H:%M:%S")
print("Current Time =", current_time)
schedule.every(5).seconds.do(loaddata)
while 1:
schedule.run_pending()
time.sleep(1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.