国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

詳解配置Django的Celery異步之路踩坑

2020-02-15 23:47:02
字體:
來源:轉載
供稿:網友

人生苦短,我用python。

看到這句話的時候,感覺可能確實是很深得人心,不過每每想學學,就又止步,年紀大了,感覺學什么東西都很慢,很難,精神啊注意力啊思維啊都跟不上。今天奶牛來分享自己今天踩的一個坑。

先說說配置過程吧,初學Django,啥都不懂,當然,python也很水,啥東西都得現查現用。Django安裝還是很簡單的。

apt-get install python3pip3 install django

嗯,就是兩條命令的事兒。

再說celery的安裝:

pip3 install celerypip3 install redis==2.10.6

目前奶牛所在的時間redis for python的版本是redis-3.0.1,為什么要用2.10.6呢?因為3.0.1壓根配置就無法運行!!!

繼續安裝redis server

apt-get install redisservice redis start

然后就可以按照celery的官方教程走了,放個URL:http://docs.celeryproject.org/en/latest/django/index.html

python3 manage.py startproject nenewcd nenewpython3 manage.py startapp nenewapptouch ./nenew/celery.pytouch ./nenewapp/tasks.py

然后增加nenew/nenew/celery.py內容為

from __future__ import absolute_import, unicode_literalsimport osfrom celery import Celery# set the default Django settings module for the 'celery' program.os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'nenew.settings')app = Celery('nenew')# Using a string here means the worker doesn't have to serialize# the configuration object to child processes.# - namespace='CELERY' means all celery-related configuration keys#  should have a `CELERY_` prefix.app.config_from_object('django.conf:settings', namespace='CELERY')# Load task modules from all registered Django app configs.app.autodiscover_tasks()@app.task(bind=True)def debug_task(self):  print('Request: {0!r}'.format(self.request))

增加nenew/nenew/__init__.py的內容

from __future__ import absolute_import, unicode_literals# This will make sure the app is always imported when# Django starts so that shared_task will use this app.from .celery import app as celery_app__all__ = ('celery_app',)

增加nenew/nenewtest/tasks.py的內容

# Create your tasks herefrom __future__ import absolute_import, unicode_literalsfrom celery import shared_task@shared_taskdef add(x, y):  return x + y@shared_taskdef mul(x, y):  return x * y@shared_taskdef xsum(numbers):  return sum(numbers)

在nenew/nenew/settings.py中增加和修改

...ALLOWED_HOSTS = ['*']....INSTALLED_APPS = [...  'nenewtest',]...CELERY_BROKER_URL = 'redis://localhost:6379/1'CELERY_RESULT_BACKEND = ‘redis://localhost:6379/0'

在nenew/nenewtest/views.py中增加或修改為

from django.shortcuts import renderfrom django.http import HttpResponsefrom .tasks import add# Create your views here.def nenewtest(request):  result = add.delay('2','2')  result.ready()  return HttpResponse('nenew Django Celery worker run !')            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 临武县| 阿克苏市| 县级市| 柯坪县| 绵阳市| 邳州市| 稷山县| 泰兴市| 威信县| 和顺县| 大庆市| 弋阳县| 霍城县| 肇庆市| 新民市| 康平县| 安新县| 临湘市| 古蔺县| 三原县| 界首市| 遵义县| 海安县| 平武县| 元谋县| 红桥区| 卢湾区| 鹤壁市| 合山市| 双流县| 新绛县| 洛浦县| 陕西省| 汉沽区| 湖州市| 宜丰县| 吉木乃县| 兰西县| 廉江市| 安宁市| 冕宁县|