本文介绍了与gevent在应用程序环境之外工作的烧瓶蓝图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过烧瓶邮件与gevent的gesk异步发送电子邮件。我正在在应用程序环境之外工作。我知道与app.app_context(),但我不能让它与我的设置工作。

我的应用程序是用这样的应用程序工厂创建的:



myproject / run_dev.py

  from gevent .wsgi从my_project.app导入WSGIServer 
从my_project.config导入create_app
导入DevConfig
$ b app = create_app(DevConfig)
http_server = WSGIServer(('', 5000),app)
http_server.serve_forever()


$ b myproject / myproject / app.py

  def create_app(config = None,app_name = None,blueprints = None):
app = Flask(app_name)
configure_app(app,config)
< other stuff>
返回应用程序

以及用于发送电子邮件的代码:



myproject / myproject / mymodule / views.py
$ b

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

myproject / myproject / mymodule / utils.py

  def do_stuff(东西):
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])
返回包装

我试着添加: / p>

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

传给send_async_email(),然后我得到


$ b


解决方案为了历史的目的:把放在 send_async_email 里面的myproject.app import create_app ,而不是在其余的进口的顶部解决了ImportError,因为它导致了循环依赖。

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

解决方案

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.

这篇关于与gevent在应用程序环境之外工作的烧瓶蓝图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 10:21