reference
celery
pip install celery==4.1.1
https://www.cnblogs.com/wdliu/p/9530219.html
https://www.jianshu.com/p/6f8576a37a3e
定时:
https://blog.csdn.net/Leo062701/article/details/90613651
https://blog.csdn.net/sicofield/article/details/50937338
Configuration
dj_01/dj_01/__init__.py
1 import pymysql 2 from .celery import app as celery_app 3 4 pymysql.install_as_MySQLdb() 5 __all__ = ['celery_app']
dj_01/dj_01/celery.py
1 from __future__ import absolute_import, unicode_literals 2 import os 3 from celery import Celery 4 from django.conf import settings 5 # set the default Django settings module for the 'celery' program. 6 os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'dj_01.settings') 7 8 app = Celery('dj_01',backend='amqp', broker='amqp://guest:guest@localhost:5672//') 9 app.config_from_object('django.conf:settings') 10 app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)
dj_01/dj_01/setting.py
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'mytest',
)
1
# Celery 设置 2 # CELERY_BROKER_URL = 'redis://localhost:6379/0' 3 BROKER_URL = 'amqp://guest:[email protected]:5672//' 4 CELERY_ACCEPT_CONTENT = ['json'] 5 CELERY_TASK_SERIALIZER = 'json' 6 CELERY_RESULT_SERIALIZER = 'json' 7 # CELERY_RESULT_BACKEND = 'django-db' 8 CELERY_TIMEZONE = 'Asia/Shanghai' 9 10 from datetime importtimedelta . 11 12 is # timer task 13 is CELERYBEAT_SCHEDULE = { 14 ' the Add-Every-30-seconds The ' : { 15 ' Task ' : ' mytest.tasks.add ' , # task name 16 ' Schedule ' : timedelta (seconds The = 2), # each time performing the task 2s . 17 ' args ' : (2,. 3 ) 18 is } . 19 }
Asynchronous tasks
Services: celery -A dj_01 worker -l info
app/tasks.py
. 1 from Time Import SLEEP 2 from Celery Import shared_task . 3 . 4 @shared_task # Celery -A -l dj_01 worker info . 5 DEF celeryTest (AGS *, ** kwargs): . 6 Print ( ' consuming operations tasks ... Start ' ) . 7 Print (AGS) . 8 Print (kwargs) . 9 SLEEP (10 ) 10 Print ( ' consuming operations tasks ... End ' )
app/view.py
from .tasks import celeryTest
1 def testt(req): 2 d = { 3 "result":'123123', 4 } 5 6 res = celeryTest.delay(json.dumps(d)) 7 print("res:%s"%res) #b9020fb2-61c1-494d-8e66-a86776943c76
8 return render(req, 'acc_login.html')
Regular tasks
Services: celery -A dj_01 beat -l info
https://blog.csdn.net/Leo062701/article/details/90613651
https://blog.csdn.net/sicofield/article/details/50937338