python celery异步_【Python】Celery异步处理
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模塊
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的python celery异步_【Python】Celery异步处理的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 学python还是不会编程_你真的不学P
- 下一篇: websocket python爬虫_p