提前谢谢,我想找一个有点celery 味的家庭主妇。我解决不了我的问题,已经4个小时了,我在想怎么回事,但我说不出来。今天早上一切都很好,但现在我在提交表单时面临着无限的负载。
首先,我在arch上使用每个软件包的最新版本,下面是我从终端收到的代码和消息:
配置:
演示/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'),
}
}
.环境:
REDIS_HOST=localhost
REDIS_PORT=6379
REDIS_CELERY_DB=0
我的模特书/模特:
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"]
我的demo/tasks.py(我在同一个文件中混合了模型和f表单,因为它只是为了测试目的)
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))
我的演示/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()
最后是我的观点: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)
我在shell中运行的是:(sudo systemctl start redis)然后是celery -a demo worker-l debug
输出:
-------------- 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 花-演示
[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']
当我提交表单时,我得到了无休止的加载,没有错误消息,flower的 Jmeter 板上什么也没有。今天早上它运行得很好,我第一次认为我在保存时把代码弄乱了,但是我在不同的虚拟环境中从头重写了两次,得到了相同的结果,我还从arch存储库重新安装了redis。因为我对celery/redis不太了解,所以我认为有一些任务在队列中等待,阻止了其他任务,所以我键入了一些命令来清除,但是没有任何帮助,如果yoiu有线索,请提前感谢!!
2条答案
按热度按时间pftdvrlh1#
切换到rabbitmq,它似乎工作完美,celery 经纪人amqp://localhost'+像这里解释的那样启用服务器https://wiki.archlinux.org/index.php/rabbitmq#installation
mgdq6dx12#
我用这样的超基本函数测试过:
def\u视图(请求):
我也有同样的无休止的加载,所以我想redis肯定有问题,我想我要把我的电脑恢复到今天早上的rsync状态