Celery任务未执行



我正在学习本教程:http://blog.miguelgrinberg.com/post/using-celery-with-flask。我已经导入了所有的包,运行应用程序时没有任何错误。但当我按下发送时,我的邮件帐户中没有收到任何邮件。我在send_async_email中添加了一个打印语句。芹菜任务似乎没有执行。我的代码:

app = Flask(__name__)
app.config['CELERY_BROKER_URL'] = 'redis://localhost:6379/0'
app.config['CELERY_RESULT_BACKEND'] = 'redis://localhost:6379/0'
celery = Celery(app.name, broker=app.config['CELERY_BROKER_URL'])
celery.conf.update(app.config)
app.config['SECRET_KEY'] = 'super_secret_key'
app.config['MAIL_SERVER'] = 'smtp.gmail.com'
app.config['MAIL_PORT'] = 465
app.config['MAIL_USE_SSL'] = True
app.config['MAIL_USERNAME'] = 'my_mail@gmail.com'
app.config['MAIL_PASSWORD'] = 'password'
app.config['MAIL_DEFAULT_SENDER'] = 'sender@gmail.com'
mail = Mail(app)    
@celery.task
def send_async_email(msg):
    print "msg sent"
    with app.app_context():
        mail.send(msg)

@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
    # send the email
    msg = Message('Hello from Flask',
                  sender=app.config['MAIL_USERNAME'],
                  recipients=['ikram.tanjib@gmail.com'])
    msg.body = 'This is a test email sent from a background Celery task.'
    if request.form['submit'] == 'Send':
        # send right away
        send_async_email.apply_async(args=[msg])
        flash('Sending email to {0}'.format(email))
    else:
        # send in one minute
        send_async_email.apply_async(args=[msg], countdown=60)
        flash('An email will be sent to {0} in one minute'.format(email))
    return redirect(url_for('index'))
if __name__ == '__main__':
    app.run(debug=True)

编辑:解决了我自己的问题我没有启动芹菜工人venv/bin/celery worker -A app.celery --loglevel=info

在项目文件夹中激活了virtualenv并运行了芹菜工人venv/bin/celery worker -A app.celery --loglevel=info。现在它正在发挥作用。

相关内容

  • 没有找到相关文章

最新更新