Django Celery Redis 异步执行任务demo实例

it2024-09-28  23

一、windows中安装redis

 安装过程见 《在windows x64上部署使用Redis》

二、环境准备

requirements.txt

Django==1.10.5 celery==3.1.23 redis==2.10.5

 注意,celery 4.x 以上不支持windows

pip install -r requirements.txt

三、创建Django项目celery_proj,创建APP:celery_demo

>>django-admin startproject celery_proj >>cd celery_proj >>django-admin startapp celery_demo

四、添加celery相关配置信息

1.在celery_proj/celery_proj目录下,添加如下 celery.py 文件

#!/usr/bin/env python # -*- 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 'celery_proj' program. os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'celery_proj.settings') app = Celery('celery_proj') # 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))

2.在celery_proj/celery_proj/__init__.py 中添加:

from .celery import app as celery_app

3.在项目配置文件中,如celery_proj/celery_proj/settings.py 中添加:

# CELERY STUFF BROKER_URL = 'redis://localhost:6379' CELERY_RESULT_BACKEND = 'redis://localhost:6379' CELERY_ACCEPT_CONTENT = ['application/json'] CELERY_TASK_SERIALIZER = 'json' CELERY_RESULT_SERIALIZER = 'json' CELERY_TIMEZONE = 'Africa/Nairobi'

五、创建异步运行任务tasks

1.在APP目录下,如celery_proj/celery_demo目录下添加如下 tasks.py

#!/usr/bin/env python # -*- coding: utf-8 -*- import time from celery import task @task() def add(x, y): return x + y @task def run_test_suit(ts_id): print "++++++++++++++++++++++++++++++++++++" print('jobs[ts_id=%s] running....' % ts_id) time.sleep(10.0) print('jobs[ts_id=%s] done' % ts_id) result = True return result

2.在views视图中,添加相关视图,如celery_proj/celery_demo/views.py:

from django.http import HttpResponse from .tasks import run_test_suit def tasks(request): print('before run_test_suit') result = run_test_suit.delay('110') print('after run_test_suit') return HttpResponse("job is runing background~")

3.添加url映射,如修改 celery_proj/celery_proj/urls.py:

from django.conf.urls import url from celery_demo import views urlpatterns = [ url(r'^', views.tasks, name='task'), ]

4.启动django server

>>python manage.py runserver

5.启动celery worker

>>celery -A celery_proj worker -l info

当显示如下,证明worker已启动成功:

E:\workspace\celery_proj>celery -A celery_proj worker -l info -------------- celery@WINDOWS-QKKYZQB v3.1.23 (Cipater) ---- **** ----- --- * *** * -- Windows-7-6.1.7601-SP1 -- * - **** --- - ** ---------- [config] - ** ---------- .> app: celery_proj:0x33fc190 - ** ---------- .> transport: redis://localhost:6379// - ** ---------- .> results: redis://localhost:6379/ - *** --- * --- .> concurrency: 4 (prefork) -- ******* ---- --- ***** ----- [queues] -------------- .> celery exchange=celery(direct) key=celery [tasks] . celery_demo.tasks.add . celery_demo.tasks.run_test_suit . celery_proj.celery.debug_task [2017-03-16 18:32:47,885: INFO/MainProcess] Connected to redis://localhost:6379//

注意:每当tasks.py 修改时,都要重新启动worker

6.访问浏览器:http://localhost:8000/

django控制台马上输出相关打印语句并且返回 HttpResponse 响应

但是实际上 run_test_suit 任务未完成,任务在后台运行 模拟sleep 10s后才完成,运行结果可通过celery worker 控制台输出中显示。

 


***微信扫一扫,关注“python测试开发圈”,了解更多测试教程!***

转载于:https://www.cnblogs.com/guanfuchang/p/6561034.html

最新回复(0)