我必须在django项目中使用rabbitmq在芹菜中创建一个任务。基本上该项目需要发送推送通知。管理员有两个选项可以发送推送通知
1)立即发送:当他们点击推送通知立即发送
2)稍后发送:在这种情况下,管理员设置日期和时间,我需要 仅在该日期和时间发送推送通知。
我的tasks.py是
from celery.task import task
from celery.task.schedules import crontab
from datetime import timedelta
from celery.decorators import periodic_task
#@task
@periodic_task(run_every=(crontab(minute='*/1')))
def multiply(x, y):
multiplication = x * y
print('print me here')
return multiplication
celery.py是
from __future__ import absolute_import
import os
from celery import Celery
from django.conf import settings
# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings')
app = Celery('mazda')
# Using a string here means the worker will not have to
# pickle the object when using Windows.
app.config_from_object('django.conf:settings')
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)
#s@app.task(bind=True)
def debug_task(self):
print('Request: {0!r}'.format(self.request))
我正在从像result = multiply.delay(3,5)
当我运行命令celery -A {myappname} worker -l info
时,我可以看到任务是在celery上创建的,另外一个队列还在rabbitmq队列上生成(这里我无法检查是否创建了相同的任务)
现在我的问题是如何在管理员从管理员方面选择的时间和日期上调用此任务。所有文件都说延迟功能,没有什么能让我选择设置任务的确切延迟。请帮帮我。
settings.py文件是
import os
#import djcelery
#djcelery.setup_loader()
# 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.10/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'bjn4ngz(mk*$*(z12jk+ztf4b*-p@b42l#)rind!cl)$%z49h^'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = [
'mazda.apps.MazdaConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'push_notifications',
'djcelery'
#'djng',
]
PUSH_NOTIFICATIONS_SETTINGS = {
"GCM_API_KEY": "AIzaSyC4YNDJBVJK75QEVWuMKPXuTgiIEfa26Pw",
"APNS_CERTIFICATE": "pushLatestPushFile.pem",
# "WNS_PACKAGE_SECURITY_ID": "",
# "WNS_SECRET_KEY": "",
}
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'mysite.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',
],
},
},
]
WSGI_APPLICATION = 'mysite.wsgi.application'
# Database
# https://docs.djangoproject.com/en/1.10/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'django',
'USER': 'postgres',
'PASSWORD': 'admin123',
'HOST': '',
'PORT': '',
}
}
# Password validation
# https://docs.djangoproject.com/en/1.10/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.10/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.10/howto/static-files/
STATIC_URL = '/static/'
LOGIN_URL = '/mazda/userlogin'
BROKER_URL = 'amqp://guest:guest@localhost:5672//'
CELERY_TIMEZONE = 'Europe/London'
答案 0 :(得分:0)
将apply_async()
与eta
参数一起使用(eta必须是日期时间对象,指定确切的日期和时间,包括毫秒精度和时区信息):
>>> from datetime import datetime, timedelta
>>> tomorrow = datetime.utcnow() + timedelta(days=1)
>>> multiply.apply_async((3, 5), eta=tomorrow)
芹菜文档here中的更多信息。