我正在尝试运行这个非常简单的烧瓶/芹菜应用程序:
@celery.task
def send_async_email(msg):
with app.app_context():
mail.send(msg)
@celery.task(bind=True)
@app.route('/', methods=['GET', 'POST'])
def index():
if request.method == 'GET':
return render_template('index.html', email=session.get('email', ''))
email = request.form['email']
session['email'] = email
msg = Message('Hello from Flask',
recipients=[request.form['email']])
msg.body = 'This is a test email sent from a background Celery task.'
print type(msg)
print dir(msg)
print 'msg.send'
print msg.send
print 'msg'
print msg
if request.form['submit'] == 'Send':
send_async_email.delay(msg)
flash('Sending email to {0}'.format(email))
else:
return redirect(url_for('index'))
但它不起作用,因为此 Message 类的实例不是 json 可序列化的,似乎并非如此:
msg.send
<bound method Message.send of <flask_mail.Message object at 0x7ffa94cd0290>>
msg
Content-Type: text/plain; charset="utf-8"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Subject: Hello from Flask
From: flask@example.com
EncodeError: <flask_mail.Message object at 0x7ffa94cd0290> is not JSON serializable