您的位置:首页 > 编程语言 > Python开发

【Python】Celery异步处理

2016-07-26 10:38 585 查看
参考: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安装,我用django1.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"isdeprecatedinfavorof"requires_system_checks".
RemovedInDjango19Warning)


所有包pip安装最新版后解决,不过django升级后会有部分低版本语法兼容问题。

在使用celery时发现几个注意点:

1、异步功能方法必须放在tasks.py中

2、异步方法不能封装在类中

启动worker时提示不可以超管用户启动worker,可以设置:

[root@testproject]#pythonmanager.pyceleryworker-linfo
Runningaworkerwithsuperuserprivilegeswhenthe
workeracceptsmessagesserializedwithpickleisaverybadidea!

IfyoureallywanttocontinuethenyouhavetosettheC_FORCE_ROOT
environmentvariable(butpleasethinkaboutthisbeforeyoudo).

Userinformation:uid=0euid=0gid=0egid=0

[root@test/data]#exportC_FORCE_ROOT="true"



定时方法

打开django管理界面/admin/djcelery/periodictask/可以直接设置

使用pythonmanage.pycelerybeat开启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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: