2016-04-13 21 views
-2

Bu eğiticiyi takip ediyorum: http://blog.miguelgrinberg.com/post/using-celery-with-flask. Tüm paketleri içe aktardım ve uygulamayı çalıştırdığımda hatam yok. Ama ben bastığımda, posta hesabımda posta alamıyorum. send_async_email'da bir baskı bildirimi ekledim. Görünüşe göre kereviz görevi çalışmıyor. Kodum:Kereviz görevi yürütülüyor

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) 

Düzenleme: kereviz işçisi venv/bin/celery worker -A app.celery --loglevel=info başlamadı kendi sorunu çözüldü.

+2

Kereviz işçilerini mi çalıştırıyorsunuz? – davidism

+0

@tanjibpa çözümünüzü cevap olarak gönderin ve "kabul edildi" olarak işaretleyin –

+0

Teşekkürler. Onu şimdi buldum. Şimdi çalışıyor. :) – tanjibpa

cevap

1

Proje klasöründe virtualenv etkinleştirildi ve kereviz işçisi venv/bin/celery worker -A app.celery --loglevel=info çalıştı. Ve şimdi çalışıyor.