Create and use a decorator for background functions

This commit is contained in:
Patrick Jentsch
2020-04-21 18:34:21 +02:00
parent 79c9ca97b2
commit bc27744946
12 changed files with 131 additions and 133 deletions

View File

@ -1,7 +1,7 @@
from flask import current_app, render_template
from flask_mail import Message
from threading import Thread
from . import mail
from .decorators import background
def create_message(recipient, subject, template, **kwargs):
@ -15,13 +15,7 @@ def create_message(recipient, subject, template, **kwargs):
return msg
@background
def send(app, msg):
with app.app_context():
mail.send(msg)
def send_async(msg):
app = current_app._get_current_object()
thread = Thread(target=send, args=(app, msg))
thread.start()
return thread