21

I use:

  1. Celery
  2. Django-Celery
  3. RabbitMQ

I can see all my tasks in the Django admin page, but at the moment it has just a few states, like:

  • RECEIVED
  • RETRY
  • REVOKED
  • SUCCESS
  • STARTED
  • FAILURE
  • PENDING

It's not enough information for me. Is it possible to add more details about a running process to the admin page? Like progress bar or finished jobs counter etc.

I know how to use the Celery logging function, but a GUI is better in my case for some reasons.

So, is it possible to send some tracing information to the Django-Celery admin page?

4

4 回答 4

31

这是我使用您的设置的最小进度报告 Django 后端。我仍然是 Django n00b,这是我第一次搞乱 Celery,所以这可能可以优化。

from time import sleep

from celery import task, current_task
from celery.result import AsyncResult

from django.http import HttpResponse, HttpResponseRedirect
from django.core.urlresolvers import reverse
from django.utils import simplejson as json
from django.conf.urls import patterns, url


@task()
def do_work():
    """ Get some rest, asynchronously, and update the state all the time """
    for i in range(100):
        sleep(0.1)
        current_task.update_state(state='PROGRESS',
            meta={'current': i, 'total': 100})


def poll_state(request):
    """ A view to report the progress to the user """
    if 'job' in request.GET:
        job_id = request.GET['job']
    else:
        return HttpResponse('No job id given.')

    job = AsyncResult(job_id)
    data = job.result or job.state
    return HttpResponse(json.dumps(data), mimetype='application/json')


def init_work(request):
    """ A view to start a background job and redirect to the status page """
    job = do_work.delay()
    return HttpResponseRedirect(reverse('poll_state') + '?job=' + job.id)


urlpatterns = patterns('webapp.modules.asynctasks.progress_bar_demo',
    url(r'^init_work$', init_work),
    url(r'^poll_state$', poll_state, name="poll_state"),
)
于 2013-02-27T16:39:50.717 回答
10

我开始尝试自己解决这个问题。首先按照Celery用户指南中的说明定义一个 PROGRESS 状态,然后您只需在模板中插入一个 js 来更新您的进度条。

于 2011-10-21T07:58:41.840 回答
6

感谢@Florian Sesser 的示例!

我制作了一个完整的 Django 应用程序,在http://iambusychangingtheworld.blogspot.com/2013/07/django-celery-display-progress-bar-of.html向用户显示创建 1000 个对象的进度

每个人都可以下载和使用它!

于 2013-07-05T14:09:37.967 回答
6

我会为此推荐一个名为celery-progress的库。它旨在尽可能轻松地将基本的端到端进度条设置放入 django 应用程序中,并尽可能少地使用脚手架,同时如果需要还支持前端的大量定制。在 README 中开始使用的大量文档和参考资料。

完全披露:我是该库的作者/维护者。

于 2018-06-26T16:21:46.973 回答