问题
我有一个Django站点,使用celeri+RabbitMQ作为长时间运行任务的任务队列。我把结果存储在Redis中。我已经能够使用Celery的update_state在引导进度条中显示一个任务的进度,并通过一个按钮向Redis DB发送ajax post来检索当前状态。在
理想情况下,我想显示每个任务当前正在运行或最近完成,是在Redis与它自己的进度条。目前,我只能显示由我的小Click Here按钮启动的当前任务的进度。在
我试着为进度条制作多个类,但老实说,我不知道该怎么做,似乎找不到任何关于如何做这样的事情的东西。我试着上传尽可能多的代码。任何帮助将不胜感激!在
代码
在网址.pyurlpatterns = [
url(r'^poll_state$', poll_state, name="poll_state"),
url(r'^do_task$', do_task, name="do_task"),
]
在视图.py
^{pr2}$
在任务.pyfrom __future__ import absolute_import, unicode_literals
from celery import shared_task
from celery.decorators import task
from celery import current_task
from celery.result import AsyncResult
import celery
from .celery import app
import time
#Task loops every half second to update the current state
@task(bind=True, ignore_result=True)
def add(self):
for i in range(101):
time.sleep(0.5)
self.update_state(state="PROGRESS", meta={'current': i, 'total': 100})
在芹菜from __future__ import absolute_import, unicode_literals
import os
from celery import Celery
from django.conf import settings
# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'yodaclaw.settings')
app = Celery('myAppName')
# Using a string here means the worker doesn'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')
# Load task modules from all registered Django app configs.
# This allows you in shell to not have to import yodaclaw.tasks
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)
@app.task
def debug_task(self):
print('Request: {0!r}'.format(self.request))
在设置.py# Celery Settings
CELERY_BROKER_URL = 'amqp://localhost'
CELERY_RESULT_BACKEND = 'redis://localhost:6379/0'
CELERY_ACCEPT_CONTENT = ['application/json']
CELERY_ACCEPT_CONTENT = ['json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_TRACK_STARTED = True
在基准.py{% load static %}
{% block title_outer %}
{% endblock %}
{% block meta %}
{% endblock %}
{% block stylesheets %}
{% endblock %}
{% block javascript %}
{% endblock %}
{% block extra_head %}
{% endblock %}
{% block body %}
{% block nav_header %}
{% endblock %}
{% block nav_sidebar %}
{% endblock %}
{% block content_wrapper %}
{% block content_header %}
{% endblock %}
{% block content_outer %}
{% block messages %}
{% endblock %}
{% block content_block_wrap %}
{% block content %}{% endblock %}
{% endblock %}
{% endblock %}
{% endblock content_wrapper %}
{% block nav_footer %}
{% endblock %}
{% if task_id %}
Task ID: {{ task_id }}
{% endif %}
Click here!
{% if task_id %}
jQuery(document).ready(function() {
var PollState = function(task_id) {
jQuery.ajax({
url: "poll_state",
type: "POST",
data: "task_id=" + task_id,
}).done(function(task) {
if (task.current) {
jQuery('.progress-bar').css({'width': task.current + '%'});
jQuery('.progress-bar').html(task.current + '%');
}
else {
jQuery('.status').html(task);
};
PollState(task_id);
});
}
PollState('{{ task_id }}');
})
{% endif %}
jQuery('#do-task').click( function() {
jQuery.ajax({
url: "do_task",
data: {},
success: function(){
jQuery.ajax({
url: "",
context: document.body,
success: function(s, x) {
jQuery(this).html(s);
}
});
}
})
});
{% endblock body %}
{% block extra_foot %}{% endblock %}