HOWTO django + celery + rabbitmq simple setup for testing purpose
来源:互联网 发布:淘宝宝贝排名软件 编辑:程序博客网 时间:2024/05/22 03:07
This setup is simply used for testing purpose and using the official First Steps with Celery and First Steps with Django as reference.
Installation
Step1. Install EPEL repository
Using yum search epel
to find the exact package name for your distribution. Then use yum install
to install EPEL.
Step2. Install Erlang
Erlang is required for RabbitMQ to work. Use yum install erlang
to install it.
Step3. Install RabbitMQ
RabbitMQ is the message broker we chose. Use yum install rabbitmq-server
to install it.
Step4. Install Celery
The installation of Celery is very easy. Just use pip install celery
Step5. Install Django
The installation of Django is also very easy. Just use pip install django
Configuration
First, setup a very basic Django project django-admin startproject proj
Then create a new proj/proj/celery.py module that defines the Celery instance:
file:proj/proj/celery.pyfrom __future__ import absolute_importimport osfrom celery import Celery# set the default Django settings module for the 'celery' program.os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'proj.settings')from django.conf import settingsapp = 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))
Then you need to import this app in your proj/proj/__init__.py
module. This ensures that the app is loaded when Django starts so that the @shared_task
decorator (mentioned later) will use it:
File:proj/proj/__init__.pyfrom __future__ import absolute_import# 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
Next, create a demoapp
app in your proj
project, using python manage.py startapp demoapp
Next, create a tasks.py
in demoapp
IMPORTANT: Make sure the file name is tasks.py
. Otherwise, it will not be autodiscovered.
The tasks you write will probably live in reusable apps, and reusable
apps cannot depend on the project itself, so you also cannot import
your app instance directly.The @shared_task decorator lets you create tasks without having any
concrete app instance:
demoapp/tasks.py:from __future__ import absolute_importfrom 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)
Next, configure the broker and backend for Celery. To do this, since we’ve already told Celery to look for configuration in proj/settings.py
, we will add the following to that file.
BROKER_URL = 'amqp://guest:guest@localhost:5672//'CELERY_RESULT_BACKEND = 'amqp'
Test
RabbitMQ
start rabbitmq and run it in background, rabbitmq-server -detached
stop rabbitmq, rabbitmqctl stop
check rabbitmq status, rabbitmqctl status
Celery
Start celery worker process, celery -A proj worker -l info
IMPORTANT:To test your @shared_task in tasks.py, you must using “python manage.py shell” to start a python shell. That will load the configuration in proj/_init_.py
Otherwise, if you start a python shell normally using “python”, then configuration in proj/_init_ will not be read, thus @shared_task will not function properly. For example, shared_task.ready() will raise AttributeError: ‘DisabledBackend’ object has no attribute ‘_get_task_meta_for’
- HOWTO django + celery + rabbitmq simple setup for testing purpose
- Django + Celery + Rabbitmq setting
- celery + rabbitmq + django
- django+celery+RabbitMQ 环境配置
- Django+Rabbitmq+Celery 框架学习
- 使用 django+celery+RabbitMQ 实现异步执行
- 使用django+celery+RabbitMQ实现异步执行
- Django 1.5.4 专题23 Celery RabbitMQ
- 使用 django+celery+RabbitMQ 实现异步执行
- python安装包制作|Django+Celery+Rabbitmq
- 使用django+celery+RabbitMQ实现异步执行
- Nginx+uwsgi+django+RabbitMQ+celery安装配置
- OJ2.0 Django+Rabbitmq+Celery配置
- simple script for loop testing
- HowTO: Setup Alerts for Portal Home Page "Listings"
- Howto | Setup an ARM Linux system for QEMU
- Celery(3):Celery+Django
- Celery-4.1 用户指南:Testing with Celery
- 嵌入式软件开发概述(4.1)------嵌入式软件程序设计
- HDU 1232 畅通工程
- hdu 3065 AC自动机 匹配串编号以及出现次数
- 一切开始,无论怎样,希望自己坚持下去
- String字符串方法详解
- HOWTO django + celery + rabbitmq simple setup for testing purpose
- 职责链模式(Chain of Responsibility)
- OpenCV window vs2012编译问题拾遗
- 程序算法艺术与实践:基础知识之函数的渐近的界
- 挑7
- C++文件读写函数之——fopen、fread和fwrite、fgetc和fputc、fgets和fputs、ftellf和fseek、rewind
- 嵌入式程序设计语言(4.2)------嵌入式软件程序设计
- 零基础学python-3.3 标识符
- java中大数处理和高精度小数处理(so easy)