标签:用途 ons 原理 tin with worker python函数 tiny print
1.安装:
pip install celery celery-with-redis django-celery
2.用途
为了提升用户体验,使用celery可以异步执行耗时或者定时的操作,views可以先返回view执行结果
3.操作原理:
4.操作步骤
参考文献:http://docs.jinkan.org/docs/celery/
https://www.cnblogs.com/znicy/p/5626040.html
1.settings
INSTALLED_APPS = [ ‘django.contrib.admin‘, ‘django.contrib.auth‘, ‘django.contrib.contenttypes‘, ‘django.contrib.sessions‘, ‘django.contrib.messages‘, ‘django.contrib.staticfiles‘, ‘booktest‘, ‘tinymce‘, ‘haystack‘, ‘djcelery‘,
import djcelery
djcelery.setup_loader()
# os.environ.setdefault(‘DJANGO_SETTINGS_MODULE‘, ‘test6.settings‘)
BROKER_URL = ‘redis://127.0.0.1:6379/0‘
CELERY_IMPORTS = (‘booktest.task‘)
2.在应用目录创建task.py文件:
import django django.setup() import time from celery import task @task def sayhello(): print(‘hello ...‘) time.sleep(15) print(‘world ...‘)
3.view中添加task任务
from .task import * ... sayhello.delay()
4.
python manage.py migrate
python manage.py celery worker --loglevel=info
注意:python manage celery work执行出现django路径错误,可以在task.py中导入django的setting
标签:用途 ons 原理 tin with worker python函数 tiny print
原文地址:https://www.cnblogs.com/yitiaodahe/p/9346741.html