我试图让一个警报功能在Flask中运行,其基本思想是使该功能将任务添加到调度模块中,以便它将在后台运行并最终显示一条消息。但是,当我提交表单时,页面只是试图无休止地加载新任务,直到到达时间为止。我以为它必须与插入函数的位置有关,但我不确定在哪里插入。
def alarm():
now = time.time()
date_time = request.form['alarm']
date_format = '%Y-%m-%dT%H:%M'
alarm_for = int(time.mktime(time.strptime(date_time,date_format)))
run_for = int(alarm_for) - int(now)
s.enter(run_for,1,alarm_msg)
while True:
s.run(blocking=False)
@app.route('/',methods=['POST','GET']) #adding two methods posting and getting
def index():
if request.method == 'POST': #submitting form
task_content = request.form['content'] #create new task from user input
alarm_content = request.form['alarm']
alarm_tmp = alarm_content.replace('T', '-').replace(':', '-').split('-') #converting databse to python, help from stack-overflow
alarm_tmp = [int(v) for v in alarm_tmp]
alarm_datetime = datetime(*alarm_tmp)
new_task = Table(content=task_content,alarm=alarm_datetime) #have the contents = input
alarm()
try:
db.session.add(new_task) #add to database
db.session.commit()
return redirect('/') #redirect to input page
except:
return 'There was an error'
else:
tasks = Table.query.order_by(Table.date_created).all() #showing all contents on site
return render_template('index.html',tasks=tasks) #user viewing the page