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


currenttime

will be evaluated on the first load of the script, but will not be over-evaluated on concurrent calls api_seconds

because your application is already loaded. You can move the computation currenttime

inside a method api_seconds

and it has to be done every time.



+3


source







All Articles