Current time using time.time () does not change when updated [Python]
I am using a module time
to convert n milliseconds to seconds and add it to the current time.
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)
This successfully returns the time during the script run, but does not change to the current time after the update. I have to stop the script and re-run it for the time to be updated. How can I display the current time? Thanks in advance.
+3
source to share
2 answers
You install currenttime
when the application flask
starts and is not updated until the application restarts. I would put currenttime
in your route function
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)
+3
source to share