django使用supervisor管理celery
supervisor配置文件如下:
[program:celery]
;
Set full path to celery program if using virtualenv
command=bash celery.sh or python manage.py celery worker or...directory=/home/ubuntu/python/waterwxapi/
user=root
numprocs=1
stdout_logfile=/var/log/supervisor/celery.log
stderr_logfile=/var/log/supervisor/celery_error.log
autostart=true
autorestart=true
startsecs=10;
Need to wait for currently executing tasks to finish at shutdown.
;
Increase this if you have very long running tasks.
stopwaitsecs = 600;
When resorting to send SIGKILL to the program to terminate it
;
send SIGKILL to its whole process group instead,
;
taking care of its children as well.
killasgroup=true
;
Set Celery priority higher than default (999)
;
so, if rabbitmq is supervised, it will start first.
priority=1000
django配置
pip install django-celeryCelerycelery-with-redis
settings.py
在INSTALLED_APPS中加入app:INSTALLED_APPS = (
...
'djcelery',
}import djcelery
djcelery.setup_loader()BROKER_URL = 'redis://127.0.0.1:6379/0'
CELERY_ACCEPT_CONTENT = ['pickle', 'json', 'msgpack', 'yaml']
【django使用supervisor管理celery】在某个apps的目录下新建一个tasks.py
如果用户是非root用户,而supervisor是 ROOT 用户进行的,所以要在tasks.py中加上
from celery import Celery,platforms
platforms.C_FORCE_ROOT = True
tasks.py
#coding=utf-8
from celery import Celery,platforms
from config import bind_device
app = Celery('tasks', broker='redis://localhost:6379//')
platforms.C_FORCE_ROOT = True
@app.task
def sendMesg(event):
...codes
sudo supervisorctl 重新restart项目
推荐阅读
- 由浅入深理解AOP
- 【译】20个更有效地使用谷歌搜索的技巧
- mybatisplus如何在xml的连表查询中使用queryWrapper
- MybatisPlus|MybatisPlus LambdaQueryWrapper使用int默认值的坑及解决
- MybatisPlus使用queryWrapper如何实现复杂查询
- django-前后端交互
- iOS中的Block
- Linux下面如何查看tomcat已经使用多少线程
- 使用composer自动加载类文件
- android|android studio中ndk的使用