2016-08-22 33 views
0

我試圖讓Django不要等到發送郵件。我決定使用芹菜來做到這一點。不幸的是,我無法弄清楚如何使它異步工作。使用芹菜發送異步郵件不起作用

我創建了一個文件tasks.py

# -*- coding: utf-8 -*- 
from __future__ import absolute_import 
from .notifications import CustomerNotifications 
from celery import shared_task 

@shared_task 
def prereservation_has_been_confirmed(reservation): 
    CustomerNotifications.prereservation_has_been_confirmed(reservation) 

CustomerNotifications.prereservation_has_been_confirmed(reservation)方法發送一封電子郵件客戶。

電子郵件發送工作,但它仍然等待,直到電子郵件發送。 (該視圖由AJAX調用)。

你知道該怎麼辦?

編輯

這是函數是如何被稱爲:

@csrf_exempt 
def reservation_confirm(request): 
    if request.method == 'POST': 
     reservation_id = request.POST.get('reservation_id', False) 
     reservation = get_object_or_404(dolava_models.Reservation, id=reservation_id) 
     reservation.confirmed = True 
     reservation.save() 
     tasks.prereservation_has_been_confirmed(reservation) 
     return JsonResponse({}) 
    raise Http404(u'...') 

我試圖tasks.prereservation_has_been_confirmed.delay(reservation)但它返回連接已被拒絕。

SETTINGS.PY - 添加BROKER_URL = 'django://''kombu.transport.django'並遷移。

# -*- coding: utf-8 -*- 

""" 
Django settings for dolava project. 

Generated by 'django-admin startproject' using Django 1.9.7. 

For more information on this file, see 
https://docs.djangoproject.com/en/1.9/topics/settings/ 

For the full list of settings and their values, see 
https://docs.djangoproject.com/en/1.9/ref/settings/ 
""" 

import os 

# Build paths inside the project like this: os.path.join(BASE_DIR, ...) 
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 


# Quick-start development settings - unsuitable for production 
# See https://docs.djangoproject.com/en/1.9/howto/deployment/checklist/ 

# SECURITY WARNING: keep the secret key used in production secret! 
SECRET_KEY = 'secret_key' 

# SECURITY WARNING: don't run with debug turned on in production! 
DEBUG = True 

ALLOWED_HOSTS = [] 


# Application definition 

INSTALLED_APPS = [ 
    'admin_interface', 
    'flat', 
    'colorfield', 
    'django.contrib.admin', 
    'django.contrib.auth', 
    # 'djcelery_email', 

    'django.contrib.contenttypes', 
    'django.contrib.sessions', 
    'django.contrib.messages', 
    'django.contrib.staticfiles', 
    'dolava_app', 
    # 'constance', 

    # 'constance.backends.database', 
    'solo', 
    'django_extensions', 
    'django_tables2', 
    'django_countries', 
    'kombu.transport.django' 
] 

MIDDLEWARE_CLASSES = [ 
    'django.middleware.security.SecurityMiddleware', 
    'django.contrib.sessions.middleware.SessionMiddleware', 
    'django.middleware.common.CommonMiddleware', 
    'django.middleware.csrf.CsrfViewMiddleware', 
    'django.contrib.auth.middleware.AuthenticationMiddleware', 
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware', 
    'django.contrib.messages.middleware.MessageMiddleware', 
    'django.middleware.clickjacking.XFrameOptionsMiddleware', 

] 

ROOT_URLCONF = 'dolava.urls' 

TEMPLATES = [ 
    { 
     'BACKEND': 'django.template.backends.django.DjangoTemplates', 
     'DIRS': [], 
     'APP_DIRS': True, 
     'OPTIONS': { 
      'context_processors': [ 
       'django.template.context_processors.debug', 
       'django.template.context_processors.request', 
       'django.contrib.auth.context_processors.auth', 
       'django.contrib.messages.context_processors.messages', 
       'django.core.context_processors.media', 
      ], 
     }, 
    }, 
] 

WSGI_APPLICATION = 'dolava.wsgi.application' 
BROKER_URL = 'django://' 

# Database 
# https://docs.djangoproject.com/en/1.9/ref/settings/#databases 

DATABASES = { 
    'default': { 
     'ENGINE': 'django.db.backends.sqlite3', 
     'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), 
    } 
} 


# Password validation 
# https://docs.djangoproject.com/en/1.9/ref/settings/#auth-password-validators 

AUTH_PASSWORD_VALIDATORS = [ 
    { 
     'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', 
    }, 
    { 
     'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', 
    }, 
    { 
     'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', 
    }, 
    { 
     'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', 
    }, 
] 


# Internationalization 
# https://docs.djangoproject.com/en/1.9/topics/i18n/ 

LANGUAGE_CODE = 'en-us' 

TIME_ZONE = 'UTC' 

USE_I18N = True 

USE_L10N = True 

USE_TZ = True 


# Static files (CSS, JavaScript, Images) 
# https://docs.djangoproject.com/en/1.9/howto/static-files/ 

STATIC_URL = '/static/' 
PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__)) 
STATIC_ROOT = os.path.join(PROJECT_ROOT, 'static') 

# CELERY_ALWAYS_EAGER = True 
# SMTP SETTINGS 
# EMAIL_BACKEND = 'djcelery_email.backends.CeleryEmailBackend' 
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend' 
# EMAIL_BACKEND = "mailer.backend.DbBackend" 
EMAIL_USE_TLS = True 
EMAIL_HOST = 'smtp.gmail.com' 
EMAIL_PORT = 587 
EMAIL_HOST_USER = '[email protected]' 
EMAIL_HOST_PASSWORD = 'pass' 




MEDIA_ROOT = os.path.join(BASE_DIR, 'media') 
MEDIA_URL = '/media/' 
+0

@Alasdair我在問題的底部添加了視圖。 –

+1

你必須使用delay()。如果它說連接已被拒絕,那麼這就是你必須解決的問題。竇你有你的芹菜實例運行?檢查你的經紀人的連接。也許創建另一個帖子與您的堆棧回溯 – pleasedontbelong

+0

@pleasedontbelong我沒有設置經紀人。現在,我已經設置了它並且延遲發送不起作用,但是在控制檯中我看不到任何錯誤。 –

回答

2

[關注評論]您的任務已正確發送給代理(至您的案例中的數據庫),您可以檢查您的未決任務是否存在。

您在控制檯中看不到任何東西,因爲您必須啓動一個芹菜工作器,它將讀取代理上的所有待處理任務並執行它們。 have you started the worker process?你會看到正在調用的任務和結果或回溯如果出現問題

1

你最好不要讓異步調用prereservation_has_been_confirmed()以延緩函數執行使用prereservation_has_been_confirmed.delay(...)

@csrf_exempt 
def reservation_confirm(request): 
    if request.method == 'POST': 
     reservation_id = request.POST.get('reservation_id', False) 
     reservation = get_object_or_404(dolava_models.Reservation, id=reservation_id) 
     reservation.confirmed = True 
     reservation.save() 

     # change HERE, you are calling your function directly not asynchronously 
     tasks.prereservation_has_been_confirmed(reservation) 
     # change to below 
     # tasks.prereservation_has_been_confirmed.delay(reservation) 
     return JsonResponse({}) 
    raise Http404(u'...') 

除此之外,您還沒有配置正確芹菜在Django應用程序使用。 請參閱Django celery first steps