工程结构说明:源文件下载请访问https://i.cnblogs.com/Files.aspxpython
__init__.py:实例化celery,并加载配置模块redis
celeryconfig.py:配置模块app
task1:任务1,实现加法spa
task2:任务2,实现乘法3d
app.py:应用,任务生产者日志
一、__init__.py:实例化celery,并加载配置模块code
# -*- coding: utf-8 -*- from celery import Celery myapp=Celery('demo') #经过Celery实例加载配置模块celeryconfig.py myapp.config_from_object('celerywithconfig.celeryconfig')
二、celeryconfig.py:配置模块blog
# -*- coding: utf-8 -*- ''' Created on 2019年8月28日 @author: lenovo ''' BROKER_URL='redis://localhost:6379/1' CELERY_RESULT_BACKEND='redis://localhost:6379/2' CELERY_TIMEZONE='Asia/Shanghai'#不指定时区的话默认采用UTC #导入指定的任务模块 CELERY_IMPORTS=( 'celerywithconfig.task1', 'celerywithconfig.task2', )
三、task1:任务1,实现加法ip
# -*- coding: utf-8 -*- ''' Created on 2019年8月28日 @author: lenovo ''' import time #从__init__.py中导入实例化的Celery myapp from celerywithconfig import myapp @myapp.task def add(x,y): time.sleep(3) return x+y
四、task2:任务2,实现乘法utf-8
# -*- coding: utf-8 -*- ''' Created on 2019年8月28日 @author: lenovo ''' import time from celerywithconfig import myapp @myapp.task def multiply(x,y): time.sleep(4) return x * y
五、app.py:应用,任务生产者
# -*- coding: utf-8 -*- ''' Created on 2019年8月28日 @author: lenovo ''' from celerywithconfig import task1 from celerywithconfig import task2 task1.add.delay(2, 4) task2.multiply.delay(4, 5) print 'end...'
六、启动worker,监放任务
cd到src路径下,执行命令python -m celery -A celerywithconfig worker --loglevel=info
七、执行app.py,生产任务
八、查看任务消费状况:worker日志显示同时接收到了2个任务,并分别进行了消费:
九、查看任务消费状况:消费结果成功保存在backend中: