2016-04-13 173 views
-2

我正在关注本教程: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'] = '[email protected]' 
app.config['MAIL_PASSWORD'] = 'password' 
app.config['MAIL_DEFAULT_SENDER'] = '[email protected]' 

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=['[email protected]']) 
    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

+2

您是否在运行芹菜工? – davidism

+0

@tanjibpa发送您的解答作为答案并将其标记为“已接受” –

+0

谢谢。我刚刚发现了。它正在工作。 :) – tanjibpa

回答

1

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