Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask blueprints with gevent working outside of application context

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

like image 595
alexferl Avatar asked Feb 24 '14 20:02

alexferl


1 Answers

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.

like image 177
Menno Avatar answered Oct 04 '22 20:10

Menno