這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)?lái)有關(guān)Django中如何使用django-celery完成異步任務(wù) ,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
Django中如何使用django-celery完成異步任務(wù)
我們可以使用pip在vietualenv中安裝:
pip install django-celery celery
django settings設(shè)置
import djcelery
djcelery.setup_loader()
BROKER_URL = 'redis://127.0.0.1:6379/2' ---使用redis當(dāng)消息隊(duì)列
注冊(cè)
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'devops',
'apps',
'common',
'djcelery',
]
在apps下創(chuàng)建一個(gè)task.py文件
from celery import task
@task
def add(x, y):
return x + y
@task
def pp():
return 'ffffffffffffffffffffff'
返回settings將其導(dǎo)入進(jìn)去
import djcelery
djcelery.setup_loader()
BROKER_URL = 'redis://127.0.0.1:6379/2'
CELERY_IMPORTS = ('apps.task')
隨意選個(gè)views中的方法調(diào)用改task測(cè)試下
#項(xiàng)目列表
def project_list(request):
admin = Admin.objects.get(id=get_current_admin_id(request))
pt=admin.projects.all().order_by('-id')
from apps import task
tt=task.add.delay(2,2)
print 'vvvvvvvvvvvvvvvvvvvv',tt -----訪問(wèn)該功能時(shí)調(diào)用task的add方法
page_objects = pages(pt, request, 5) ##分頁(yè)
return render_to_response('project/project_list.html',locals())
安裝啟動(dòng)redis:略
啟動(dòng)runserver 與 celery
python manage.py runserver
python manage.py celery worker --loglevel=info
[2017-11-10 14:55:17,016: INFO/MainProcess] Task apps.task.add[9bafe6d2-8411-4f5f-8eed-10444da0ae3a] succeeded in 0.00280212797225s: 4 --可以看到worker日志,返回結(jié)果 打開新的terminal, 激活virtualenv, 并切換到django項(xiàng)目目錄: $ python manage.py shell >>> from apps.task import add >>> add.delay(2, 2) 此時(shí), 你可以在worker窗口中看到worker執(zhí)行該task: [2014-10-07 08:47:08,076: INFO/MainProcess] Got task from broker: myapp.tasks.add[e080e047-b2a2-43a7-af74-d7d9d98b02fc] [2014-10-07 08:47:08,299: INFO/MainProcess] Task myapp.tasks.add[e080e047-b2a2-43a7-af74-d7d9d98b02fc] succeeded in 0.183349132538s: 4 Eager模式 如果在settings.py設(shè)置: CELERY_ALWAYS_EAGER = True 那么Celery便以eager模式運(yùn)行, 則task便不需要加delay運(yùn)行: # 若啟用eager模式, 則以下兩行代碼相同 add.delay(2, 2) add(2, 2) settings配置添加 CELERYBEAT_SCHEDULE = { 'add-every-3-minutes': { 'task': 'apps.task.add', 'schedule': timedelta(seconds=3), 'args': (16, 16) }, } CELERYBEAT_SCHEDULER = 'djcelery.schedulers.DatabaseScheduler' # 定時(shí)任務(wù) CELERYBEAT_SCHEDULE = { 'add-every-3-seconds': { 'task': 'apps.task.pp', 'schedule': timedelta(seconds=3) ---每隔3秒 執(zhí)行task下的pp函數(shù) }, } 啟動(dòng)celery beat 其實(shí)還有一種簡(jiǎn)單的啟動(dòng)方式worker和beat一起啟動(dòng): python manage.py celery worker --loglevel=info --beat 啟動(dòng)beat 執(zhí)行定時(shí)任務(wù)時(shí), Celery會(huì)通過(guò)celerybeat進(jìn)程來(lái)完成. Celerybeat會(huì)保持運(yùn)行, 一旦到了某一定時(shí)任務(wù)需要執(zhí)行時(shí), Celerybeat便將其加入到queue中. 不像worker進(jìn)程, Celerybeat只有需要一個(gè)即可. 啟動(dòng): python manage.py celery beat --loglevel=info 查看worker日志 [2017-11-10 16:17:25,853: INFO/MainProcess] Received task: apps.task.pp[8a3af6fb-5189-4647-91f2-8aa07489dd1e] [2017-11-10 16:17:25,858: INFO/MainProcess] Task apps.task.pp[8a3af6fb-5189-4647-91f2-8aa07489dd1e] succeeded in 0.00379144400358s: 'ffffffffffffffffffffff' [2017-11-10 16:17:28,858: INFO/MainProcess] Received task: apps.task.pp[d87e4ea0-8881-449a-b993-e7657f50ef25] [2017-11-10 16:17:28,864: INFO/MainProcess] Task apps.task.pp[d87e4ea0-8881-449a-b993-e7657f50ef25] succeeded in 0.0049942266196s: 'ffffffffffffffffffffff' [2017-11-10 16:17:31,859: INFO/MainProcess] Received task: apps.task.pp[4d05b4f3-92ff-4922-a8f4-7e047749239a] [2017-11-10 16:17:31,865: INFO/MainProcess] Task apps.task.pp[4d05b4f3-92ff-4922-a8f4-7e047749239a] succeeded in 0.00537821277976s: 'ffffffffffffffffffffff' [2017-11-10 16:17:34,859: INFO/MainProcess] Received task: apps.task.pp[5b21afc1-ebf1-4858-be68-20b9bf318452] [2017-11-10 16:17:34,865: INFO/MainProcess] Task apps.task.pp[5b21afc1-ebf1-4858-be68-20b9bf318452] succeeded in 0.00530335493386s: 'ffffffffffffffffffffff'
訪問(wèn)對(duì)應(yīng)的頁(yè)面看日志
手工測(cè)試task
Eager模式
dj-celery 定時(shí)任務(wù)
如果需要傳參可以這樣寫
補(bǔ)充還有另外一種方法那就是celery
上述就是小編為大家分享的Django中如何使用django-celery完成異步任務(wù) 了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)-成都網(wǎng)站建設(shè)公司行業(yè)資訊頻道。