Reputation: 4295
I am new to celery. I was following this example.
Views.py
def results(request):
documents = Document.objects.all()
if request.method == 'POST':
form = DocumentForm(request.POST, request.FILES)
if form.is_valid():
data = form.cleaned_data['name']
print 'data', data
newdoc = Document(docfile=request.FILES['docfile'])
newdoc.save()
#send it to celery
documents = Document.objects.all()
return render_to_response(
'results.html',
{'documents' : documents}, context_instance=RequestContext(request))
The user would upload a file which would be processed by an engine. In the meantime, i would want the results page to show all jobs that are already completed. I thought of using celery and modifying the tasks.py to process the files. After its completed, the file will be shown as a url for users to download it.
I cannot think of a way to poll this tasks asynchronously to achieve this part. thanks.
Upvotes: 3
Views: 3406
Reputation: 474
You can use Django-celery-results. It is an easy and nice extension of celery that enable you to store your tasks results in django database. install the extension using :
$ pip install -U django-celery-results
update settings.py :
CELERY_RESULT_BACKEND = 'django-db'
INSTALLED_APP = (
...
...
django_celery_results
)
Create the Celery database tables by performing a database migrations:
$ python manage.py migrate django_celery_results
Then you can get the list of finished tasks from the database and display it in your view.
from django_celery_results.models import TaskResult
def tasks_view(request):
tasks = TaskResult.objects.all()
template = "tasks.html"
return render(request, template, {'tasks': tasks})
Define the template "templates/tasks.html"
<html>
<head>
<title>tasks</title>
</head>
<body>
{% if tasks %}
<ul>
{% for task in tasks %}
<li> {{task.id}} : {{ task.result }}</a></li>
{% endfor %}
</ul>
<p>It works!</p>
{% else %}
<p>No tasks are available.</p>
{% endif %}
</body>
</html>
Upvotes: 8