django-celery(python2)
参考:http://docs.celeryproject.org/en/latest/getting-started/index.html
配置(项目同名文件夹下):
settings.py文件:
import os
import djcelery
djcelery.setup_loader()
BROKER_URL='redis://ip:port'
# BROKER_POOL_LIMIT=0mm
CELERY_RESULT_BACKEND='redis://ip:port'
CELERY_ACCEPT_CONTENT=['application/json']
CELERY_TASK_SERIALIZER='json'
CELERY_RESULT_SERIALIZER='json'
CELERY_TIMOZONE='Africa/Nairobi'
celery.py文件:
# -*- coding: utf-8 -*-
from __future__ import absolute_import
import os
from celery import Celery
from django.conf import settings
# set the default Django settings module for the 'myimport' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myimport.settings')
app = Celery('myimport')
# 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)
@app.task(bind=True)
def debug_task(self):
print('Request: {0!r}'.format(self.request))
__init.py__文件:
from .celery import app as celery_app
定义task.py文件:
在各个app目录下定义app文件
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from celery import task
@task(name="share_start")
def shareTask(username, website, keyword):
运行:
上图中配置好各文件后,直接在项目目录下执行命令:celery worker -A 项目同名目录名 -l info
下图中myimport为项目同名文件夹的名字(即项目名)