所以我想知道完成以下内容的最佳做法:
def execute_expensive_operation(obj):
#create instance of a model storing the relevant info to load the JSON into the template
#includea `task` field that stores the name of the asynchronous task (obj.pk) so that the JSON can be loaded if the name of the task is known
def test_view(request):
list_of_objects = [...] #will actually be dynamically loaded from the request
task_dict = {}
for obj in list_of_objects:
task_dict[obj.pk] = execute_expensive_operation(obj).delay(obj.pk) #this could take a while, put it on queue (using celery)
context = {"tasks":task_dict}
return render_to_response("template.html",context)
"template.html"
将在表格中的一行中加载与每个obj
相关的JSON
如上所述,task_dict
实际上将填充AsyncResult
个对象而不是JSON。我想要做的是,一旦与该行相关的异步任务完成,就动态加载任何行。
我为非特定方法道歉,这不是针对任何具体方面的事情,但对于我必须在各种不同的地方处理的一般情况更是如此。如果有任何遗漏的信息,请告诉我,以便我可以加入。
答案 0 :(得分:1)
这是开始的想法的要点。
<强> views.py 强>
def test_view(request):
# Normal web request processing.
# Return `json` dictionary
<强> template.html 强>
//making periodic ajax request.
(function worker() {
$.ajax({
url: 'paht/to/url',
success: function(data) {
$('.result').html(data);
},
complete: function() {
// Schedule the next request when the current one's complete
setTimeout(worker, 5000);
}
});
})();