Celery

Celery实现定时任务

1. 报错

1)celery 报错 在启动worker的时候 AttributeError: 'str' object has no attribute 'items'

redis版本问题
pip install redis==2.10.6

2)Celery Received unregistered task of type (run example)

--loglevel=DEBUG
debug模式
比较已注册的task和报错的task,找问题

3)[Django Celerybeat日志报错处理('NoneType' object has no attribute 'is_due')

按照以上意思,Django celerybeat在加载任务的时候遇见任务中有无法指定执行时间的任务,表示为NoneType类型。
数据库中djcelery_periodicttask表中,crontab_id,interval_id,两个字段都为null

4)启动celery时报错ImportError: cannot import name 'python_2_unicode_compatible'

File "/usr/local/lib/python3.6/dist-packages/django_celery_results/models.py", line 9, in <module>
    ImportError: cannot import name 'python_2_unicode_compatible'
    from celery.five import python_2_unicode_compatible
    
解决:
在/usr/local/lib/python3.6/dist-packages/django_celery_results/models.py文件中,
将from django.utils.five import python_2_unicode_compatible
换成from django.utils.six import python_2_unicode_compatible

5)No module named 'django-db'

在celery配置文件文件中添加

app.loader.override_backends['django-db'] = 'django_celery_results.backends.database:DatabaseBackend'

2. 启动

- beat:检查定时任务,并启动定时任务丢给worker执行
celery -A myproject beat -l info
python3.6 manage.py celery -A patrol beat
- worker:
celery -A myproject worker -l info
python3.6 manage.py celery -A patrol worker --loglevel=DEBUG
后台运行

3. 使用Supervisor运行celery

1)简介

supervisor是用Python开发的一个client/server服务,是Linux/Unix系统下的一个进程管理工具。可以很方便的监听、启动、停止、重启一个或多个进程。用supervisor管理的进程,当一个进程意外被杀死,supervisor监听到进程死后,会自动将它重启,很方便的做到进程自动恢复的功能,不再需要自己写shell脚本来控制。
2)配置

- yum install supervisor
安装成功后会在/etc目录中自动生成supervisord.conf文件 supervisord.d文件夹
supervisord.conf是一些默认配置,可自行修改;
supervisord.d目录用来存放用户自定义的进程配置

- 配置supervisord.conf(文件末尾)

[include]
files = relative/directory/*.ini    #可以指定一个或多个以.ini结束的配置文件

最主要配置的是supervisord.d文件夹的位置,以便supervisor控制,这里默认是supervisord.d文件夹所在位置,*.ini指的是supervisord识别所有以.ini结尾的文件,也可自定义,比如*.conf


- 运行supervisord

3)创建进程文件

在任意位置先创建一个文件,比如hello.py,待会supervisor配置的就是运行这个文件
- 进入supervisord.d文件夹,添加hello.ini

[program:hello]        名字随意,不要重复
command=python /home/hello.py 运行命令
stdout_Logfile=/home/celery_log/hello.log 进程打印的日志
stderr_logfile=/home/celery_log/hello_error.log 进程错误日志


注意: supervisor不能监控后台进程,command 不能为后台运行命令

4)运行supervisord

supervisord -c /etc/supervisord.conf 

5)常用命令

supervisorctl 是 supervisord的命令行客户端工具,直接输入:supervisorctl 进入supervisorctl 的shell交互界面,下面的命令不带supervisorctl 可直接使用

supervisorctl status:查看所有进程的状态
supervisorctl stop hello:停止es
supervisorctl start hello:启动es
supervisorctl restart hello: 重启es
supervisorctl update :配置文件修改后可以使用该命令加载新的配置
supervisorctl reload: 重新启动配置中的所有程序

把hello 换成all 可以管理配置中的所有进程

6)为celery配置ini文件并运行进程

- 在supervisord.d文件夹中创建celery_beat.ini

[program:celerybeat]
command=python3.6 manage.py celery beat -A patrol -l info
directory=/root/patrol/
user=root
autostart=true
autorestart=true
stdout_logfile=/root/patrol/celery_log/celery_baet.log
stderr_logfile=/root/patrol/celery_log/celery_beat_error.log

- 在supervisord.d文件夹中创建celery_worker.ini

[program:celeryworker]
command=python3.6 manage.py celery worker -A patrol --loglevel=info
directory=/root/patrol/
user=root
autostart=true
autorestart=true
stdout_logfile=/root/patrol/celery_log/celery_worker.log
stderr_logfile=/root/patrol/celery_log/celery_worker_error.log

- 启动worker和beat
supervisord -c /etc/supervisord.conf 
如果是在已运行的基础上新添加进程的话可使用:
supervisorctl reload

7)报错

error: <class 'OSError'>, [Errno 99] Cannot assign requested address: file: /usr/lib/python3.6/socket.py line: 713

解决:运行是要指定路径

unix:///var/run/supervisor/supervisor.sock no such file

问题描述:安装好supervisor没有开启服务直接使用supervisorctl报的错

解决办法:supervisord -c /etc/supervisord.conf

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

友情链接更多精彩内容