2016-04-13 18 views
-2

私はこのチュートリアルに従います:http://blog.miguelgrinberg.com/post/using-celery-with-flask。 私はすべてのパッケージをインポートし、私はエラーがないアプリを実行しています。しかし、私がsendを押すと、自分のメールアカウントにメールが届かない。 send_async_emailにprintステートメントを追加しました。セロリの仕事は実行されていないようです。私のコード:セロリのタスクが実行されていません

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を実行しました。そして今、それは働いています。

関連する問題