Geum
Geum

Reputation: 45

Having problems understanding celery

This is my first time learning celery and django. I have installed the latest version of celery (celery==3.1.17) and rabbitmq (amqp==1.4.6) inside my virtualenv. I'm learning from celery's website.

models.py:

from django.db import models

# Create your models here.

class Count(models.Model):
    x = models.IntegerField()

    def __unicode__(self):
        return self.x

views.py:

def home(request):
    if request.POST:
        form = CountForm(request.POST)
        if form.is_valid():
            if form.cleaned_data:
                count = form.save()
                count.x = add.delay(count.x)
                return HttpResponseRedirect('/')

    else:
        all_counts = Count.objects.all()
        form = CountForm()
    return render(request, 'home.html',{
        'form':form,
        'all_counts':all_counts
        })

template:

<body>
    <form method="post" action=".">
        {% csrf_token %}
        {{form.as_p}}
        <input type="submit" value="post">
    </form>
    {% if all_counts.count > 0 %}
        {% for count in all_counts %}
            <p>ID {{count.id}} =  :: Value = {{count.x}}</p>
            <br/>
        {% endfor %}
    {% else %}
        <p>No counts</p>
    {% endif %}
</body>

Update

tasks.py:

@app.task
def add(x):
    while x <= 50:
        return x + 1
        time.sleep(3)

What I want to perform through celery is to add the x of Count till it is equal to 50, so that the each calculation and result value will be stored asynchronously. Therefore, after each 3 seconds I should see the value of count.x changing asynchronously till the value is 50. But in the template i'm getting the same value what I posted. What am I missing? Will you please help me to understand. Thank you.

Upvotes: 2

Views: 215

Answers (2)

Alvaro
Alvaro

Reputation: 12037

Celery is not meant for this.

What you are trying to do might be achieved with pure js or even with an ajax django view.

You can use a timer in js to make async calls to the server and ask the current value, every 3 seconds. Then, you should update the html accordingly, using js or even JQuery.

Check out Django REST Framework. It's pretty convenient for working with async calls to the server.

What Celery is for

Celery is for heavy background tasks that can be performed either by a different machine or async, so that the request doesn't time out on high processing time or the resources are better managed. It won't do anything related to your template rendering, as its mostly a task queue utility.

Upvotes: 2

tuomur
tuomur

Reputation: 7098

Calling add.delay starts an asynchronous task in the worker, and immediately returns AsyncResultobject. If you want to access the actual return value of the task, you need to call AsyncResult.get(). This will block until the task is completed in the worker.

return_value = add.delay(count.x).get()

More details in the documentation.

Upvotes: 1

Related Questions