博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
django celery使用
阅读量:7231 次
发布时间:2019-06-29

本文共 2285 字,大约阅读时间需要 7 分钟。

版本:

celery:3.1.25

django-celery:3.2.2

django:1.8.16

安装celery3

1
2
pip 
install 
celery==3.1.25
pip 
install 
django-celery

celery与django结合使用的配置:

参考文档:

proj/proj/settings配置:

1
2
3
4
5
6
7
8
9
10
INSTALLED_APPS 
= 
(
    
'django.contrib.admin'
,
    
'django.contrib.auth'
,
    
'django.contrib.contenttypes'
,
    
'django.contrib.sessions'
,
    
'django.contrib.messages'
,
    
'django.contrib.staticfiles'
,
    
'study'
,
    
'djcelery'
,
)

1
2
3
4
5
6
7
8
9
10
11
12
13
import 
djcelery
djcelery.setup_loader()
BROKER_URL 
= 
'redis://172.16.42.128:6379'
CELERYBEAT_SCHEDULER 
= 
'djcelery.schedulers.DatabaseScheduler'
CELERY_RESULT_BACKEND 
= 
'djcelery.backends.database:DatabaseBackend'
CELERY_ACCEPT_CONTENT 
= 
[
'application/json'
]
CELERY_TASK_SERIALIZER 
= 
'json'
CELERY_RESULT_SERIALIZER 
= 
'json'
CELERY_TIMEZONE 
= 
'Asia/Shanghai'
CELERY_ENABLE_UTC 
= 
False
CELERYD_CONCURRENCY 
= 
10
CELERYD_MAX_TASKS_PER_CHILD 
= 
5
CELERY_SEND_EVENTS 
= 
True

proj/proj/__init__.py

1
2
3
4
5
from 
__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  
# noqa

proj/proj/celery.py

注意:proj改成你自己项目的名称

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
from 
__future__ 
import 
absolute_import
 
import 
os
 
from 
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 
settings  
# noqa
 
app 
= 
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))

demoapp/tasks.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
from 
__future__ 
import 
absolute_import
 
from 
celery 
import 
shared_task
 
 
@shared_task
def 
add(x, y):
    
return 
+ 
y
 
@shared_task
def 
mul(x, y):
    
return 
* 
y
     
     
@shared_task
def 
xsum(numbers):
    
return 
sum
(numbers)

命令行启动celery

启动celerycam

1
python manage.py celerycam

启动worker

1
 
python manage.py celery worker -l info

启动beat

1
 
python manage.py celery beat -l info
本文转自 曾哥最爱 51CTO博客,原文链接:http://blog.51cto.com/zengestudy/2069055,如需转载请自行联系原作者
你可能感兴趣的文章
jaxws-webservice编程
查看>>
网众安装U盘带WINPE和MaxDOS
查看>>
Linux man文档英语单词
查看>>
oracle的权限和角色区别
查看>>
组策略管理——软件限制策略(4)
查看>>
tcp的三次握手
查看>>
u盘中的ubuntu为了减少日志系统频繁写文件所修改的/etc/fstab
查看>>
RAID浅谈
查看>>
Map接口
查看>>
IntelliJ IDEA 使用教程
查看>>
『高级篇』docker之服务发现、部署更新和扩容(七)
查看>>
『高级篇』docker之了解CICD和DevOps(41)
查看>>
shell内置命令和外部命令区别
查看>>
AD域管理浅谈
查看>>
包含目录、库目录、附加包含目录、附加库目录、附加依赖项
查看>>
Apache+SVN+Review Board代码审核服务器搭建流程
查看>>
esproc vs python 5
查看>>
分布式系统下的哈希一致性算法设计
查看>>
NFS存储服务部署(上)
查看>>
dd测试硬盘性能
查看>>