- 我在django项目中使用的celery来执行耗时任务,启动celery时使用supervisor来管理
- 使用supervisor的方法
(1) 安装 supervisor
pip install supervisor
(2)
在项目目录下执行命令
echo_supervisord_conf > supervisord.conf
生成配置文件,然后将下面的配置信息加入
[program:celery]
;后台运行的形式启动celery
command=celery multi start w1 -A proj -l info
;非守护进程的形式启动
;command=celery -A proj worker -l info
directory=/xxx/xxx/xxx/proj ;项目目录
autostart=true
autorestart=true
redirect_stderr=true
startsecs=3 ;自动重启间隔
;celery 的日志文件,注意supervisor的注释用分号
stdout_logfile=/xxxx/xxxx/xxxxx/celery_log.log
stderr_logfile=/xxxx/xxxx/xxxx/celery_err.log
在项目目录下执行 supervisord 启动,就可以了
3. 然后我就可以执行正常的celery任务了,但是后来我新加入了一个异步任务,启动时就报错,提示:
raceback (most recent call last):
File "/home/linkdata/.local/lib/python3.5/site-packages/redis/connection.py", line 392, in read_response
raise socket.error(SERVER_CLOSED_CONNECTION_ERROR)
OSError: Connection closed by server.
KeyError: 'struction.tasks.transform_video_format'
提示我找不到这个任务,后来突然想起来,官方文档中一句话,说更改了配置文件,要重新加载一下supervisor,否则改的信息就不生效,我就想我新加的异步任务也可能没有被supervisor读到,没有生效,所以才会找不到,然后我就把supervisor 和 celery 的进程杀了,重启一下supervisor 和我的项目 就好了
这个错误不一定就只有这个问题导致的,我是根据自己的实际情况找到的问题,如果这样不行还可能是celery配置问题,建议看看官方文档,我也是看的官方文档配置的,上面介绍的还是挺清楚的.
Received task: struction.tasks.transform_video_format[a77c802e-5b48-496c-99c8-1c81039b7f87]