django、celery、django-celery、kombu、amqp最好统一pip安装,我用django 1.8.6和较低版本celery时候出现报错:
/usr/local/lib/python2.7/site-packages/Django-1.8.6-py2.7.egg/django/core/management/base.py:260: RemovedInDjango19Warning: "requires_model_validation" is deprecated in favor of "requires_system_checks".
RemovedInDjango19Warning)
如果报找不到django的错误也应该是celery装的版本不兼容导致:
[root@1 /]# python manage.py
Traceback (most recent call last):
File"manage.py", line 22, in execute_from_command_line(sys.argv)
File"/usr/lib64/python2.7/site-packages/django/core/management/__init__.py", line 364, inexecute_from_command_line
utility.execute()
File"/usr/lib64/python2.7/site-packages/django/core/management/__init__.py", line 338, inexecute
django.setup()
File"/usr/lib64/python2.7/site-packages/django/__init__.py", line 27, insetup
apps.populate(settings.INSTALLED_APPS)
File"/usr/lib64/python2.7/site-packages/django/apps/registry.py", line 85, inpopulate
app_config=AppConfig.create(entry)
File"/usr/lib64/python2.7/site-packages/django/apps/config.py", line 127, increate
import_module(entry)
File"/usr/lib64/python2.7/importlib/__init__.py", line 37, inimport_module
__import__(name)
ImportError: No module named django
建议:pip install celery==3.1.25 django-celery==3.2.2 kombu amqp django==1.11.13
所有包pip安装最新版后解决,不过django升级后会有部分低版本语法兼容问题。
在使用celery时发现几个注意点:
1、异步功能方法必须放在tasks.py中
2、异步方法不能封装在类中
启动worker时提示不可以超管用户启动worker,可以设置:
[root@test project]# python manager.py celery worker -l infoRunning a worker with superuser privileges when the
worker accepts messages serialized with pickle is a very bad idea!If you really want to continuethenyou have to set the C_FORCE_ROOT
environment variable (but please think about this before youdo).
User information: uid=0 euid=0 gid=0 egid=0
[root@test /data]# export C_FORCE_ROOT="true"
定时方法
打开django管理界面/admin/djcelery/periodictask/可以直接设置
使用python manage.py celery beat开启beat服务
注意:
1、名称要和方法名对应,比如app中background的tasks中的fun_test方法,Task(registered)中选择这个方法
2、由于队列处理是每5秒,所以Interval中如果设置小于5秒会出现每5秒执行多次
异步操作
redis存储python对象使用pickle模块