python - Redis 后端不工作?

标签 python django redis celery

我在我的 Django 项目中使用 Celery 和 Redis。我正在尝试从我的 celery_app 延迟 10 秒调用过期函数。每当该函数执行时,页面只是说 localhost 没有响应。我认为是因为我的后端不工作?有人可以帮忙吗? 附言如果我删除带有结果的 2 行,则 get 方法有效

celery_app.py

from __future__ import absolute_import
import os
from celery import Celery

from django.conf import settings
from celery.schedules import crontab

# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'IrisOnline.settings')

app = Celery('IrisOnline', broker='redis://localhost:6379/0',backend="redis://localhost:6379/0",include=[
    "IrisOnline.tasks",
    "order_management.tasks"
])

app.conf.update(
    task_serializer='json',
    accept_content=['json'],
    result_serializer='json',
    timezone='Asia/Manila',
)
app.conf.beat_schedule = {
    'add-every-30-seconds': {
        'task': 'IrisOnline.tasks.printthis',
        'schedule':(crontab(hour=13,minute=33)),
    },
}

app.config_from_object('django.conf:settings')

app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)

@app.task(name="expire")
def expire():
    print("object expired")

View .py

def get(request):

    if "approved_cart" not in request.session or not request.session["approved_cart"]:
        return redirect("/checkout/cart/")

    cart = request.session["cart"]
    customer = Customer.objects.get(user=request.user)
    order = Order.objects.create(customer=customer)

    result = expire.apply_async(countdown=40) #these causes the errors
    result.get()


    for product_id, quantity in cart.items():
        # Deduct from inventory
        product = Product.objects.get(id=product_id)
        product.quantity -= quantity
        product.save()

        # Add quantity to order
        OrderLineItems.objects.create(
            product=Product.objects.get(id=product_id),
            quantity=quantity,
            parent_order=order
        )

    request.session["cart"] = {}  # Empty cart
    request.session["approved_cart"] = False
    request.session.modified = True
    context = make_context(request)
    context["total_price"] = order.total_price

    return render(request, 'purchase.html', context)

最佳答案

当您调用 result.get() 时,这实际上是在等待任务完成 - 请参阅 Celery documentation .

所以在您的情况下,这最多会等待 40 秒,因为您设置了 countdown=40。我认为大多数浏览器等待超时的时间超过 40 秒,所以这可能不是您的问题。

但是您真的应该问问自己:如果您要在启动该任务的代码中等待它的结果,为什么还要运行一个单独的任务?

回到您的问题 - 该错误表明您的单独任务未运行。调试它的一种方法是在您的设置中设置 CELERY_ALWAYS_EAGER = True。这使得任务在调用进程中立即运行。如果代码在这些条件下工作,那么问题很可能是你的 celery worker 没有运行。

关于python - Redis 后端不工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45268096/

相关文章:

api - 用于 API 调用的 Redis

ruby-on-rails - 如何在生产中部署 resque worker?

python - 为什么在 while 循环中尝试覆盖变量时会出现内存错误? (Python)

python - botocore.exceptions.ClientError : An error occurred (AccessDenied) when calling the PutObject operation: Access Denied

python - 使用 web2py 向外键添加空值时出现 IntegrityError

python - 使用 Django 时如何处理 dyno 重启?

python - 如何在 Django 中传递参数到登录 View ,同时从 settings.py 中的登录 url 反转?

redis - 在 getServerSideProps 中使用 redis 会导致错误 net.isIP is not a function

python - 如何让 Maven 与 Python 子进程一起工作?

python - 将空值绑定(bind)到 pyodbc 中的日期字段