python celery异步_【Python】Celery异步处理
django、celery、django-celery、kombu、amqp最好統(tǒng)一pip安裝,我用django 1.8.6和較低版本celery時(shí)候出現(xiàn)報(bào)錯(cuò):
/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)
如果報(bào)找不到django的錯(cuò)誤也應(yīng)該是celery裝的版本不兼容導(dǎo)致:
[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時(shí)發(fā)現(xiàn)幾個(gè)注意點(diǎn):
1、異步功能方法必須放在tasks.py中
2、異步方法不能封裝在類中
啟動worker時(shí)提示不可以超管用戶啟動worker,可以設(shè)置:
[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"
定時(shí)方法
打開django管理界面/admin/djcelery/periodictask/可以直接設(shè)置
使用python manage.py celery beat開啟beat服務(wù)
注意:
1、名稱要和方法名對應(yīng),比如app中background的tasks中的fun_test方法,Task(registered)中選擇這個(gè)方法
2、由于隊(duì)列處理是每5秒,所以Interval中如果設(shè)置小于5秒會出現(xiàn)每5秒執(zhí)行多次
異步操作
redis存儲python對象使用pickle模塊
與50位技術(shù)專家面對面20年技術(shù)見證,附贈技術(shù)全景圖總結(jié)
以上是生活随笔為你收集整理的python celery异步_【Python】Celery异步处理的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 学python还是不会编程_你真的不学P
- 下一篇: python如何开启多进程_python