2016-04-13 226 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。現在它正在工作。