参考:http://www.cnblogs.com/znicy/p/5626040.html
参考:http://www.weiguda.com/blog/73/
参考:http://blog.csdn.net/iloveyin/article/details/44940931
django、celery、django-celery、kombu、amqp最好统一pip安装,我用django 1.8.6和较低版本celery时候出现报错:
/usr/local/lib/python2./site-packages/Django-1.8.-py2..egg/django/core/management/base.py:: RemovedInDjango19Warning: "requires_model_validation" is deprecated in favor of "requires_system_checks".
RemovedInDjango19Warning)
如果报找不到django的错误也应该是celery装的版本不兼容导致:
[root@ /]# python manage.py
Traceback (most recent call last):
File "manage.py", line , in <module>
execute_from_command_line(sys.argv)
File "/usr/lib64/python2.7/site-packages/django/core/management/__init__.py", line , in execute_from_command_line
utility.execute()
File "/usr/lib64/python2.7/site-packages/django/core/management/__init__.py", line , in execute
django.setup()
File "/usr/lib64/python2.7/site-packages/django/__init__.py", line , in setup
apps.populate(settings.INSTALLED_APPS)
File "/usr/lib64/python2.7/site-packages/django/apps/registry.py", line , in populate
app_config = AppConfig.create(entry)
File "/usr/lib64/python2.7/site-packages/django/apps/config.py", line , in create
import_module(entry)
File "/usr/lib64/python2.7/importlib/__init__.py", line , in import_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 info
Running a worker with superuser privileges when the
worker accepts messages serialized with pickle is a very bad idea! If you really want to continue then you have to set the C_FORCE_ROOT
environment variable (but please think about this before you do). User information: uid= euid= gid= 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秒执行多次
异步操作
参考:http://www.cnblogs.com/ifkite/p/4257721.html
参考:http://www.cnblogs.com/lianzhilei/p/7133295.html
redis存储python对象使用pickle模块
参考:http://jingyan.baidu.com/article/a681b0de0e88003b184346b6.html