Gevent checkboxes working out of application context

I am trying to send asynchronous email messages to Flask with gevent via flash drive. I get "work out of application context". I know with app.app_context (), but I can't get it to work with my setup.

My application is created with a factory application as follows:

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 tried to add:

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

for send_async_email (), but then I get

ImportError: cannot create create_app name

+3
source share
2 answers

: from myproject.app import create_app send_async_email , ImportError, .

+2

@copy_current_request_context decorator, ( ).

: flask.copy_current_request_context.

+1

All Articles