使用time.time()的当前时间不';刷新时不更改[Python]
我正在使用使用time.time()的当前时间不';刷新时不更改[Python],python,time,flask,Python,Time,Flask,我正在使用time模块将n毫秒转换为秒,并将其添加到当前时间 from flask import Flask currenttime = int(round(time.time() * 1000)) @app.route('/api/<seconds>') def api_seconds(seconds): milliseconds = int(seconds) * 1000 finaltime = int(currenttime + milliseconds)
time
模块将n毫秒转换为秒,并将其添加到当前时间
from flask import Flask
currenttime = int(round(time.time() * 1000))
@app.route('/api/<seconds>')
def api_seconds(seconds):
milliseconds = int(seconds) * 1000
finaltime = int(currenttime + milliseconds)
return 'Seconds: ' + seconds + '<br />' + 'Milliseconds: ' + str(milliseconds) + \
'<br />' + 'Time: ' + str(currenttime) + '<br />' + 'Final Time: ' + str(finaltime)
从烧瓶导入烧瓶
currenttime=int(四舍五入(time.time()*1000))
@app.route(“/api/”)
def api_秒(秒):
毫秒=整数(秒)*1000
finaltime=int(当前时间+毫秒)
返回'Seconds:'+Seconds+'
'+'毫秒:'+str(毫秒)+\
“
”+”时间:“+str(当前时间)+”
“+”最后时间:“+str(最终时间)
这将成功返回运行脚本时的时间,但刷新时不会更改为当前时间。我必须停止脚本并重新运行它,以便有时间刷新。如何使其显示当前时间?提前感谢。
currenttime
将在首次加载脚本时进行评估,但不会在同时调用api_seconds
时重新评估,因为您的应用程序已加载。您可以将currenttime
的计算移到api_seconds
方法中,每次都应执行该方法。当flask
应用程序运行时,您正在设置currenttime
,直到应用程序重新启动才更新。我会尝试将currenttime
放入您的路由函数中
from flask import Flask
import time
@app.route('/api/<seconds>')
def api_seconds(seconds):
currenttime = int(round(time.time() * 1000))
milliseconds = int(seconds) * 1000
finaltime = int(currenttime + milliseconds)
return 'Seconds: ' + seconds + '<br />' + 'Milliseconds: ' + str(milliseconds) + \
'<br />' + 'Time: ' + str(currenttime) + '<br />' + 'Final Time: ' + str(finaltime)
从烧瓶导入烧瓶
导入时间
@app.route(“/api/”)
def api_秒(秒):
currenttime=int(四舍五入(time.time()*1000))
毫秒=整数(秒)*1000
finaltime=int(当前时间+毫秒)
返回'Seconds:'+Seconds+'
'+'毫秒:'+str(毫秒)+\
“
”+”时间:“+str(当前时间)+”
“+”最后时间:“+str(最终时间)
这正是问题所在。谢谢你的回答!