Jul 2, 2011

django celery views

djcelery.views
is_task_successful(request, task_id)
返回json HttpResponse
{"task": {"executed": false, "id": "8b54a341-3a17-434c-8bff-53087d62c902"}}
{"task": {"executed": true, "id": "8b54a341-3a17-434c-8bff-53087d62c902"}}
task_status(request, task_id)
返回json HttpResponse
{"task": {"status": "PENDING", "result": null, "id": "92e95a60-0cf0-4103-a52e-d35b41cb58c0"}}
{"task": {"status": "SUCCESS", "result": 7, "id": "92e95a60-0cf0-4103-a52e-d35b41cb58c0"}}

推荐使用task_status,可以直接判断 json.task.result

task_view(task)
@task_view
@task
def add_view(x, y):
    x = eval(x)
    y = eval(y)
    return x + y
将task变成view,返回json HttpResponse。
{"ok": "true", "task_id": "bb265fcd-edfa-4d7d-8a55-6bd50437a72b"}
task的参数在request中提取,如
http://url/?x=4&y=9
注意类型是字符串,如果需要其他类型,可以使用eval的技巧。
使用task_view配合 urlpatterns相当于对外开放task api,这样是否有风险

0 comments: