skip to Main Content

Thanks in advance, i’m trying to get a litlle bit familliar with celery bit I can’t solve my problem, it’s been 4 hours i’m trying to figure what’s wrong but I can’t tell. Everything was working fine this morning but now i’m facing an infinite loading while submitting a form.

first of all I’m on arch and using last version of each package, here’s my code and messages i get from the terminal :

configuration :

demo/settings/base.py :

INSTALLED_APPS = [
'users.apps.UsersConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'core',
'books',
'django_celery_results',]

 CELERY_RESULT_BACKEND = 'django-db'
 CELERY_BROKER_URL = f'redis://{config("REDIS_HOST")}:{config("REDIS_PORT")}/{config("REDIS_CELERY_DB")}'

DATABASES = {
'default': {
    'ENGINE': 'django.db.backends.sqlite3',
    'NAME': os.path.join(BASE_DIR, 'db_celery__.sqlite3'),
}
}

.env :

REDIS_HOST=localhost
REDIS_PORT=6379
REDIS_CELERY_DB=0

my models books/models :

from django.db import models
from PIL import Image
from django import forms
from demo import tasks


def resize_model(booquin):
    img = Image.open(bouquin.cover.path)

    if img.height > 300 or img.width > 300:
        output_size = (300, 300)
        img.thumbnail(output_size)
        img.save(bouquin.cover.path)
        bouquin.save()


class Bouquin(models.Model):
    titre = models.CharField(max_length=200)
    cover = models.ImageField(upload_to="cover")

def __str__(self):
    return self.titre


 class Bouquin_Form(forms.ModelForm):
    def save(self, commit=True):
        book = super().save(commit)
        tasks.resize_book_celery.apply_async((book.id,))
        return book

     class Meta:
       model = Bouquin
       fields = ["titre", "cover"]

my demo/tasks.py ( I’m mixing up models andf forms in the same file cause it’s just for testing purpose)

from celery import shared_task
from books import models


@shared_task
def resize_book_celery(book_id: int):
    models.resize_model(models.Bouquin.objects.get(id=book_id))

my demo/celery.py:

from __future__ import absolute_import, unicode_literals
import os
from celery import Celery


os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'demo.settings.base')

app = Celery('demo')

app.config_from_object('django.conf:settings', namespace='CELERY')

app.autodiscover_tasks()

and finally my view : books/views.py

from django.shortcuts import render, reverse, redirect
from .models import Bouquin, Bouquin_Form
from django.contrib import messages


def create_book(request):
    book_listing = Bouquin.objects.all()

    if request.method == 'POST':
        form = Bouquin_Form(request.POST, request.FILES)
        if form.is_valid():
            form.save()
            return redirect("create-book")

    else:
        form = Bouquin_Form()

    context = {"book_listing": book_listing, "form": form}

    return render(request, "demo/book_listing.html", context)

What I run in the shell 🙁 sudo systemctl start redis ) then celery -A demo worker -l debug

output :

 -------------- celery@laptop v4.4.6 (cliffs)
 --- ***** ----- 
 -- ******* ---- Linux-5.7.9-arch1-1-x86_64-with-glibc2.2.5 2020-07-20 17:23:51
 - *** --- * --- 
 - ** ---------- [config]
 - ** ---------- .> app:         demo:0x7f0d73fd67c0
 - ** ---------- .> transport:   redis://localhost:6379/0
- ** ---------- .> results:     disabled://
- *** --- * --- .> concurrency: 4 (prefork)
 -- ******* ---- .> task events: OFF (enable -E to monitor tasks in this worker)
--- ***** ----- 
  -------------- [queues]
            .> celery           exchange=celery(direct) key=celery

celery flower -A demo

[I 200720 17:26:27 command:138] Visit me at http://localhost:5555
[I 200720 17:26:27 command:145] Broker: redis://localhost:6379/0
[I 200720 17:26:27 command:146] Registered tasks: 
['celery.accumulate',
 'celery.backend_cleanup',
 'celery.chain',
 'celery.chord',
 'celery.chord_unlock',
 'celery.chunks',
 'celery.group',
 'celery.map',
 'celery.starmap',
 'demo.tasks.resize_book_celery']

When I submit the form i got an endless loading without error message and get nothing in flower’s dashboard. It was working fine this morning, I first thought i messed up with the code while saving but I rewrite it from scratch two times in different virtual environment and i get the same result, I also reinstalled redis from the arch repository.
Since I don’t really know much about celery/redis i then thought there were some tasks waiting in queue blocking the others so I typed some commands to purge, but nothing helps, thanks in advance if yoiu have a clue !!

3

Answers


  1. Chosen as BEST ANSWER

    I've tested with an ultra basic function like that to see :

    @task
    def time_celery(x):
        return x ** 2
    
    • def multiplication_view(request):

         result = time_celery.delay(2)
      
         context = {"result": result}
         return render(request, "demo/multiplications.html", context)
      

    And i got the same endless loading so I presume there is definitely a problem with redis, I think I'm gonna restore my computer to this morning state threw rsync


  2. switched to rabbitmq and it seems to work flawlessly,
    CELERY_BROKER_URL = ‘amqp://localhost’ + enabling server like explained here https://wiki.archlinux.org/index.php/RabbitMQ#Installation

    Login or Signup to reply.
  3. To Delete all data/keys of all Redis databases use FLUSHALL command. Open terminal and type

    redis-cli FLUSHALL
    

    This problem is for redis I think.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search