I have written a Flask app where a user can input a date and time. This information is stored locally on a SQL server. Once the datetime the user specifies occurs, a function which sends a text message is executed.
I expected the user to click on the submit form and trigger the async 'schedule_hello()'
function, and be redirected right away to the blog's index. However, the code below 'schedule_hello()'
doesn't run until the time specified by the user, and they aren't redirected until that time. Am I misunderstanding what async does, or have I implemented it incorrectly?
The code for the endpoint:
@bp.route('/calendar', methods=('GET', 'POST'))
def calendar():
if request.method == 'POST':
date = request.form['date']
time = request.form['time']
error = None
if not date:
error = 'Date is required.'
if not time:
error = 'Time is required.'
if error is not None:
flash(error)
else:
db = get_db()
db.execute(
'INSERT INTO post (title, body, author_id)'
' VALUES (?, ?, ?)',
(date, time, g.user['id'])
)
db.commit()
asyncio.run(schedule_hello(date, time))
return redirect(url_for('blog.index'))
return render_template('blog/calendar.html')
Here is my code for the async function:
async def schedule_hello(date, time):
datetime_str = str(date) + " " + str(time)
delta = (datetime.strptime(datetime_str, '%m/%d/%y %H:%M:%S') - datetime.now()).total_seconds() // 1
await asyncio.sleep(delta)
"Some code which will send a text message"
print('Message sent')