celery(3) celery +django

django 可以轻松跟celery结合实现异步任务,只需简单配置即可

1)目录存放,配置celery.py

你的celery配置文件和django的setting文件 放在一个目录

这时候的配置文件就有所改动了


[root@linux-node1 RestApi]# ll
total 24
-rw-r--r-- 1 root root 44 Jul 11 05:54 __init__.py
drwxr-xr-x 2 root root 4096 Jul 11 05:57 __pycache__
-rw-r--r-- 1 root root 715 Jul 11 06:58 celery.py
-rw-r--r-- 1 root root 3653 Jul 11 05:57 settings.py
-rw-r--r-- 1 root root 817 Jul 11 05:17 urls.py
-rw-r--r-- 1 root root 392 Jul 11 05:17 wsgi.py
[root@linux-node1 RestApi]# pwd
/soft/RestApi/RestApi

[root@linux-node1 RestApi]# cat celery.py 

from __future__ import absolute_import, unicode_literals
import os
from celery import Celery
 
# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'RestApi.settings')  #配置环境变量,配置你项目名字,不然会不识别
 
app = Celery('RestApi')  #给你的celery起个名字
 
# Using a string here means the worker don't have to serialize
# the configuration object to child processes.
# - namespace='CELERY' means all celery-related configuration keys
#   should have a `CELERY_` prefix.
app.config_from_object('django.conf:settings', namespace='CELERY')  ##在项目setting中连接中间商redis的配置必须 CELERY_ 这么写
 
# Load task modules from all registered Django app configs.
app.autodiscover_tasks()  #到你app下自动的发现所有的task任务
 
 
@app.task(bind=True)
def debug_task(self):
    print('Request: {0!r}'.format(self.request))

2)修改项目的__init__.py  不是app的__init__

from __future__ import absolute_import, unicode_literals
 
# This will make sure the app is always imported when
# Django starts so that shared_task will use this app.
from .celery import app as celery_app
 
__all__ = ['celery_app']

3)配置项目的setting 添加中间商redis

[root@linux-node1 RestApi]# vim settings.py

CELERY_BROKER_URL = 'redis://10.1.11.29:6379/1'
CELERY_RESULT_BACKEND = 'redis://10.1.11.29:6379/1'

CELERY_IMPORTS = (
 'pro.tasks',  #如果work检测不到你的tasks任务,那你在配置文件中导入app.tasks  

)

 

4)task文件存放

必须起名叫tasks.py   这个跟django的admin.py 是一个道理,   celery会自动的去每个app下去找你的tasks.py文件

- app1/
    - tasks.py
    - models.py
- app2/
    - tasks.py
    - models.py

 查看tasks.py

from __future__ import absolute_import, unicode_literals
from celery import shared_task

#shared_task 代表在所有的app下都可以调用这个task



@shared_task
def add(x, y):
    return x + y


@shared_task
def mul(x, y):
    return x * y


@shared_task
def xsum(numbers):
    return sum(numbers)

5)编写views

from rest import tasks
#这里只测试一个方法
def celery_call(request):
    t = tasks.add.delay(4,8)
    res = t.get()
  #ret=t.id #就是返回这次结果的id,可以根据id来取值
print(t.get()) return HttpResponse(res)

另外一种情况,根据提交过来的ID,来返回值, 还是在views中添加视图函数

from celery.result import AsyncResult

def celery_result(request):
    task_id = request.GET.get('id')
    res = AsyncResult(id=task_id)
    if res.ready():
      result = res.get()
    else:
      result = res.ready()
    return HttpResponse(result)

路由系统也需要增加,自己加吧

然后测试

获取id 

http://192.168.199.200:8080/celery_call/

结果: 14fbc3a7-ad32-4791-9b4e-0f9973c4ad08

根据ID获取值

http://192.168.199.200:8080/celery_res/?id=14fbc3a7-ad32-4791-9b4e-0f9973c4ad08
 结果:12

 

6)编写路由信息

from rest import views
urlpatterns = [
    url(r'^admin/', admin.site.urls),

    url(r'^celery_call/',views.celery_call),
]

7)启动你的django程序 和 启动你的worker

python3 manage.py runserver 0.0.0.0:8080

celery -A RestApi worker -l debug ##RestApi 你的项目 #这样它会自己去你的app下面去寻找你的tasks

8)访问你的页面,

  就会出现你的add 结果12

 

posted @ 2017-07-11 23:28  所有的梦想都画在墙上  阅读(344)  评论(0)    收藏  举报