这段代码是我的芹菜工人脚本:
from app import celery, create_app app = create_app('default') app.app_context().push()
当我尝试运行worker时,我会遇到这个错误:
File "/home/vagrant/myproject/venv/app/mymail.py", line 29, in send_email_celery msg.html = render_template(template + '.html', **kwargs) File "/home/vagrant/myproject/venv/local/lib/python2.7/site-packages/flask/templating.py", line 126, in render_template ctx.app.update_template_context(context) File "/home/vagrant/myproject/venv/local/lib/python2.7/site-packages/flask/app.py", line 716, in update_template_context context.update(func()) TypeError: 'NoneType' object is not iterable
我的问题是,在芹菜中使用工作人员时,如何发送电子邮件任务.
mymail.py
from flask import current_app, render_template from flask.ext.mail import Message from . import mail, celery @celery.task def send_async_email_celery(msg): mail.send(msg) def send_email_celery(to, subject, template, **kwargs): app = current_app._get_current_object() msg = Message(subject, sender=app.config['MAIL_SENDER'], recipients=[to]) msg.html = render_template(template + '.html', **kwargs) send_async_email_celery.delay(msg)
__在里面__
... def create_app(config_name): app = Flask(__name__) app.config.from_object(config[config_name]) config[config_name].init_app(app) bootstrap.init_app(app) mail.init_app(app) db.init_app(app) login_manager.init_app(app) celery.conf.update(app.config) redis_store.init_app(app) from .users import main as main_blueprint app.register_blueprint(main_blueprint) return app
显然,蓝图和工人之间存在一些冲突.如果可能,删除蓝图不是一个选项,因为我需要在电子邮件模板中使用自定义过滤器.
最后发现使用此代码进行一些调试后出现问题的原因是什么.
我有一个app_context_processor
不会返回任何结果.
@mod.app_context_processor def last_reputation_changes(): if current_user: #code return dict(reputation='xxx')
发送电子邮件时,current_user
需要一个else
案例来返回一些内容,因为current_user
from from flask.ext.login import current_user
未定义.基本上我只需要这样的东西.
def last_reputation_changes(): if current_user: #code return dict(reputation='xxx') else: return dict(reputation=None)
所以问题与芹菜无关,而是与烧瓶登录集成有关.