使用time.time()的当前时间在刷新时不会改变[Python]

时间:2015-07-31 00:53:37

标签: 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)
    return 'Seconds: ' + seconds + '<br />' + 'Milliseconds: ' + str(milliseconds) + \
       '<br />' + 'Time: ' + str(currenttime) + '<br />' + 'Final Time: ' + str(finaltime)

这会成功返回运行脚本时的时间,但不会更改为刷新时的当前时间。我必须停止脚本并重新运行它以便有时间刷新。如何让它显示当前时间?提前谢谢。

2 个答案:

答案 0 :(得分:3)

首次加载脚本时将评估

currenttime,但在api_seconds的并发调用中不会重新评估

currenttime,因为您的应用已加载。您可以改为移动api_seconds方法内的$(gridId).jqGrid({ colNames: ["Id", "Name", "Col 1", "Col 2", "Col 3"], colModel: [ { name: "Id", index: "Id", hidden: true, key: true, formatter: 'text' }, { name: "Name", index: "Name", width: '165px', align: 'left', resizable: false, editable: false, classes: 'non-editable', formatter: 'text' }, { name: "Col1", index: "Col1", width: '60px' }, { name: "Col2", index: "Col2", width: '60px' }, { name: "Col3", index: "Col3", width: '60px' } ], hidegrid: false, rowNum: 50, rowList: [10, 20, 50, 100], scroll: true, loadonce: true, sortorder: "asc", height: 370, datatype: 'json', viewrecords: true, mtype: 'GET', ignoreCase: true, jsonReader: { root: "rows", page: "page", total: "total", records: "records", repeatitems: false, userdata: "userdata" }, url: gridUrl, editurl: '#', cellEdit: true, cellsubmit: 'clientArray', footerrow: true, multiselect: false, //autowidth: true, cmTemplate: { //Default col model properties, if not using default, override in the colmodel resizable: false, edittype: "text", //for checkbox, override in the colmodel align: "right", editable: true, formatter: 'number', //for text, override in the colmodel formatoptions: { decimalSeparator: ".", thousandsSeparator: "", decimalPlaces: 1, defaultValue: '', align: "right" }, editrules: { number: true} //Setting numeric validation for all columns as all the editable columns are numbers }, beforeEditCell: function (rowid, cellname, value, iRow, iCol) { var grid = $(this); if (prevCellName != cellname && cellname == "Col2") { var autoCalculated = 123; var response = confirm("The Col 2 auto-calculated value is " + autoCalculated + ". Would you like to use this value?"); if (response == true) { setTimeout(function () { grid.jqGrid('setCell', rowid, "Col2", autoCalculated); }, 10); setTimeout(function () { grid.jqGrid('editCell', iRow, iCol + 1, true); }, 20); } } prevCellName = cellname; } }); 计算,并且每次都应该执行。

答案 1 :(得分:3)

您在flask应用程序运行时设置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) 放入您的路线功能

CREATE TABLE Date
(
     "year" smallint NOT NULL DEFAULT extract(year from current_date)
);