Question

I am trying to send emails asynchronously with in Flask with gevent via flask-mail. I am getting "working outside of application context". I am aware of with app.app_context() but I cannot get it to work with my setup.

My application is created with an application factory like this:

myproject/run_dev.py

from gevent.wsgi import WSGIServer
from my_project.app import create_app
from my_project.config import DevConfig

app = create_app(DevConfig)
http_server = WSGIServer(('', 5000), app)
http_server.serve_forever()

myproject/myproject/app.py

def create_app(config=None, app_name=None, blueprints=None):
    app = Flask(app_name)
    configure_app(app, config)
    <other stuff>
    return app

And the code that I use to send emails:

myproject/myproject/mymodule/views.py

@mymodule.route('/some/path/')
def do_something():
    do_stuff(something)

myproject/myproject/mymodule/utils.py

def do_stuff(something):
    send_email(msg)

@async
def send_async_email(msg):
    mail.send(msg)

def send_mail(request_id, recipients, email_type, env=None, pool=None):
    msg = Message(
        sender=sender,
        recipients=recipients,
        subject=subject,
        body=body)
    send_async_email(msg)

myproject/myproject/decorators.py

def async(f):
    def wrapper(*args, **kwargs):
        t = Greenlet.spawn(f, *args, **kwargs)
        gevent.joinall([t])
    return wrapper

I have tried to add:

from myproject.app import create_app
app = create_app()
with app.app_context():
    mail.send(msg)

to send_async_email() but then I get

ImportError: cannot import name create_app

Was it helpful?

Solution

For historical purposes: putting the from myproject.app import create_app inside the send_async_email instead of at the top of with the rest of the imports solved the ImportError, because it caused a circular dependency.

OTHER TIPS

You need to define your async function within your view and use the @copy_current_request_context decorator, so that it can have access to the current request (and app) context.

For details: documentation of flask.copy_current_request_context.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top