【Python】Celery异步处理

摘要:
参考资源:http://www.cnblogs.com/znicy/p/5626040.html参考资源:http://www.weiguda.com/blog/73/参考资源:http://blog.csdn.net/iloveyin/article/details/44940931django、cell、django cell、kombu和amqp应统一安装。我使用django 1.8.6和

参考: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.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 <module>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.25django-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 continue thenyou have to set the C_FORCE_ROOT
environment variable (but please think about this before you do).

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服务

【Python】Celery异步处理第1张

注意:

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

免责声明:文章转载自《【Python】Celery异步处理》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇ABP+AdminLTE+Bootstrap Table权限管理系统一期数据类型后面加问号是什么意思?下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Python3 -- 查看python安装路径以及pip安装的包列表及路径

1. 查看python路径终端输入命令: whereis python 假设你的电脑上同时安装了python2和python3,可以使用使用以下命令分别查看其安装路径: whereis python2 whereis python3 2.查看使用pip安装的软件包默认python3 pip list python2查看pip安装的软件包名称及版本 pyth...

白月黑羽Python在线教程

推荐白月黑羽Python在线教程 白月黑羽 站在初学者的角度为大家安排了Python学习教程,帮助大家迅速掌握程序开发技能。 http://www.python3.vip/doc/tutorial/python/home/ 变量 和 注释 本文目录 变量的概念 变量的命名规则 变量值的变化 我们已经知道,Python语言中,所有的数据类型都是对象。 整...

操蛋的Django model------select_related() 主要用于一对一和一对多

实例: 创建表,表都是一对一,一对多 class Province(models.Model): name = models.CharField(max_length=10) class City(models.Model): name = models.CharField(max_length=5) province = mode...

Django安装和web框架原理

Django安装和web框架原理 在PyCharm中安装 在cmd中输入 django-admin startproject 项目名 如果报错 不是内部或外部命令,也不是可运行的程序 需要添加环境变量 运行Django项目 切换到项目名目录下 python manage.py runserver 或者 python manage.py...

2020系统综合实践 第7次实践作业 11组

目录 1.在树莓派中安装opencv库 1.1 安装依赖 1.2 下载OpenCV源码 1.3 安装pip 1.4 安装Python虚拟机 1.5 编译OpenCV 1.6 安装OpenCV 2.使用opencv和python控制树莓派的摄像头 3.利用树莓派的摄像头实现人脸识别 facerec_on_raspberry_pi.py face...

在Linux服务器上运行Jupyter notebook server教程

在Linux服务器上运行Jupyter notebook server教程 很多deep learning教程都推荐在jupyter notebook运行python代码,方便及时交互。但只在本地运行没有GPU环境,虽然google colab是个好办法,但发现保存模型后在云端找不到模型文件,且需要合理上网才能访问。于是想给实验室的服务器配置jupyter...