Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django:celery任务不使用.delay()执行_Python_Django_Ubuntu_Celery_Orders - Fatal编程技术网

Python Django:celery任务不使用.delay()执行

Python Django:celery任务不使用.delay()执行,python,django,ubuntu,celery,orders,Python,Django,Ubuntu,Celery,Orders,我正在使用“Django 2 by Example”一书中的代码示例来构建我的电子商务商店。视图按其应该的方式工作,但任务永远不会执行。没有错误。页面冻结在“等待本地主机…”上。我在代码中做错了什么 视图.py import ... from .tasks import order_created def order_create(request): cart = Cart(request) if request.me

我正在使用“Django 2 by Example”一书中的代码示例来构建我的电子商务商店。视图按其应该的方式工作,但任务永远不会执行。没有错误。页面冻结在“等待本地主机…”上。我在代码中做错了什么

视图.py

   import ...
   from .tasks import order_created

        def order_create(request):
            cart = Cart(request)
            if request.method == 'POST':
                form = OrderCreateForm(request.POST)

                if form.is_valid():
                    order = form.save(commit=False)
                    if cart.promo_code:
                        order.promo_code = cart.promo_code
                        order.discount = cart.promo_code.discount
                    order.save()

                    for item in cart:
                        OrderItem.objects.create(order=order,
                                                 book=item['book'],
                                                 price=item['price'],
                                                 quantity=item['quantity'])
                    # clear the cart
                    cart.clear()
                    print("clean cart")
                    # set the order in the session
                    request.session['order_id'] = order.id
                    print("id  - check")
                    # launch asynchronous task
                    order_created.delay(order.id) # here I stuck
                    print("clean cart")
                    # redirect for payment
                    return redirect(reverse('payment:process'))
            else:
                form = OrderCreateForm()
            return render(request,
                          'orders/order/create.html',
                          {'cart': cart, 'form': form})
from celery import task
from django.core.mail import send_mail
from .models import Order


@task
def order_created(order_id):
    """
    Task to send an e-mail notification when an order is 
    successfully created.
    """
    order = Order.objects.get(id=order_id)
    subject = 'Order nr. {}'.format(order.id)
    message = 'Dear {},\n\nYou have successfully placed an order.\
                  Your order id is {}.'.format(order.first_name,
                                               order.id)
    mail_sent = send_mail(subject,
                          message,
                          'archeski.dk@gmail.com',
                          [order.email])
    return mail_sent
任务。py

   import ...
   from .tasks import order_created

        def order_create(request):
            cart = Cart(request)
            if request.method == 'POST':
                form = OrderCreateForm(request.POST)

                if form.is_valid():
                    order = form.save(commit=False)
                    if cart.promo_code:
                        order.promo_code = cart.promo_code
                        order.discount = cart.promo_code.discount
                    order.save()

                    for item in cart:
                        OrderItem.objects.create(order=order,
                                                 book=item['book'],
                                                 price=item['price'],
                                                 quantity=item['quantity'])
                    # clear the cart
                    cart.clear()
                    print("clean cart")
                    # set the order in the session
                    request.session['order_id'] = order.id
                    print("id  - check")
                    # launch asynchronous task
                    order_created.delay(order.id) # here I stuck
                    print("clean cart")
                    # redirect for payment
                    return redirect(reverse('payment:process'))
            else:
                form = OrderCreateForm()
            return render(request,
                          'orders/order/create.html',
                          {'cart': cart, 'form': form})
from celery import task
from django.core.mail import send_mail
from .models import Order


@task
def order_created(order_id):
    """
    Task to send an e-mail notification when an order is 
    successfully created.
    """
    order = Order.objects.get(id=order_id)
    subject = 'Order nr. {}'.format(order.id)
    message = 'Dear {},\n\nYou have successfully placed an order.\
                  Your order id is {}.'.format(order.first_name,
                                               order.id)
    mail_sent = send_mail(subject,
                          message,
                          'archeski.dk@gmail.com',
                          [order.email])
    return mail_sent
我正在使用
芹菜==4.2.0
Django==2.0.7
Ubuntu 18.04

更新: 在芹菜.py中添加了这两行代码。冻结已经消失,订单页面现在重定向到我进一步的付款程序。但是创建的任务
订单
仍然没有运行

  app.conf.broker_url = 'redis://localhost:6379/1'
    # app.conf.broker_transport_options = {'visibility_timeout': 3600}  # 1 hour.
    app.config_from_object('django.conf:settings', namespace='CELERY')
$redis cli监视器

输出如下:

OK
1531137225.134559 [1 127.0.0.1:48116] "ZREVRANGE" "book:1:purchased_with" "0" "-1"
1531137240.820217 [1 127.0.0.1:48130] "LPUSH" "celery" "{\"body\": \"W1szMV0sIHt9LCB7ImNhbGxiYWNrcyI6IG51bGwsICJlcnJiYWNrcyI6IG51bGwsICJjaGFpbiI6IG51bGwsICJjaG9yZCI6IG51bGx9XQ==\", \"content-encoding\": \"utf-8\", \"content-type\": \"application/json\", \"headers\": {\"lang\": \"py\", \"task\": \"orders.tasks.order_created\", \"id\": \"172d5ec0-9b53-4766-bc04-470631a3a8e4\", \"shadow\": null, \"eta\": null, \"expires\": null, \"group\": null, \"retries\": 0, \"timelimit\": [null, null], \"root_id\": \"172d5ec0-9b53-4766-bc04-470631a3a8e4\", \"parent_id\": null, \"argsrepr\": \"(31,)\", \"kwargsrepr\": \"{}\", \"origin\": \"gen24500@archeski-Inspiron-5558\"}, \"properties\": {\"correlation_id\": \"172d5ec0-9b53-4766-bc04-470631a3a8e4\", \"reply_to\": \"b8b0c8bb-1931-3be2-a044-d588d95c2fe9\", \"delivery_mode\": 2, \"delivery_info\": {\"exchange\": \"\", \"routing_key\": \"celery\"}, \"priority\": 0, \"body_encoding\": \"base64\", \"delivery_tag\": \"249e3deb-489b-4e0b-bde7-54160d3ccaaf\"}}"
1531137464.423873 [0 127.0.0.1:48192] "SELECT" "1"
1531137464.424085 [1 127.0.0.1:48192] "ZREVRANGE" "book:1:purchased_with" "0" "-1"
1531137479.993181 [0 127.0.0.1:48198] "SELECT" "1"
1531137479.993295 [1 127.0.0.1:48198] "PING"
1531137479.994161 [0 127.0.0.1:48200] "SELECT" "1"
1531137479.994260 [1 127.0.0.1:48200] "MULTI"
1531137479.994279 [1 127.0.0.1:48200] "LLEN" "celery"
1531137479.994286 [1 127.0.0.1:48200] "LLEN" "celery\x06\x163"
1531137479.994291 [1 127.0.0.1:48200] "LLEN" "celery\x06\x166"
1531137479.994295 [1 127.0.0.1:48200] "LLEN" "celery\x06\x169"
1531137479.994299 [1 127.0.0.1:48200] "EXEC"
1531137479.994618 [1 127.0.0.1:48200] "SADD" "_kombu.binding.celery" "celery\x06\x16\x06\x16celery"
1531137479.994977 [1 127.0.0.1:48200] "LPUSH" "celery" "{\"body\": \"W1szMl0sIHt9LCB7ImNhbGxiYWNrcyI6IG51bGwsICJlcnJiYWNrcyI6IG51bGwsICJjaGFpbiI6IG51bGwsICJjaG9yZCI6IG51bGx9XQ==\", \"content-encoding\": \"utf-8\", \"content-type\": \"application/json\", \"headers\": {\"lang\": \"py\", \"task\": \"orders.tasks.order_created\", \"id\": \"0fbcecc4-293d-472b-8308-46cb2d9ff345\", \"shadow\": null, \"eta\": null, \"expires\": null, \"group\": null, \"retries\": 0, \"timelimit\": [null, null], \"root_id\": \"0fbcecc4-293d-472b-8308-46cb2d9ff345\", \"parent_id\": null, \"argsrepr\": \"(32,)\", \"kwargsrepr\": \"{}\", \"origin\": \"gen24983@archeski-Inspiron-5558\"}, \"properties\": {\"correlation_id\": \"0fbcecc4-293d-472b-8308-46cb2d9ff345\", \"reply_to\": \"f046e6b8-dd2b-30a4-86a5-0de752377bc7\", \"delivery_mode\": 2, \"delivery_info\": {\"exchange\": \"\", \"routing_key\": \"celery\"}, \"priority\": 0, \"body_encoding\": \"base64\", \"delivery_tag\": \"088bae10-976e-4143-8c28-9ed2e254f2d5\"}}"
1531137480.232213 [1 127.0.0.1:48200] "LPUSH" "celery" "{\"body\": \"W1szM10sIHt9LCB7ImNhbGxiYWNrcyI6IG51bGwsICJlcnJiYWNrcyI6IG51bGwsICJjaGFpbiI6IG51bGwsICJjaG9yZCI6IG51bGx9XQ==\", \"content-encoding\": \"utf-8\", \"content-type\": \"application/json\", \"headers\": {\"lang\": \"py\", \"task\": \"orders.tasks.order_created\", \"id\": \"5aa54a37-95da-43c9-b59e-e6797b7cdd2f\", \"shadow\": null, \"eta\": null, \"expires\": null, \"group\": null, \"retries\": 0, \"timelimit\": [null, null], \"root_id\": \"5aa54a37-95da-43c9-b59e-e6797b7cdd2f\", \"parent_id\": null, \"argsrepr\": \"(33,)\", \"kwargsrepr\": \"{}\", \"origin\": \"gen24983@archeski-Inspiron-5558\"}, \"properties\": {\"correlation_id\": \"5aa54a37-95da-43c9-b59e-e6797b7cdd2f\", \"reply_to\": \"f046e6b8-dd2b-30a4-86a5-0de752377bc7\", \"delivery_mode\": 2, \"delivery_info\": {\"exchange\": \"\", \"routing_key\": \"celery\"}, \"priority\": 0, \"body_encoding\": \"base64\", \"delivery_tag\": \"f78cd769-d7fa-43de-aee9-f7bc5a2c78ac\"}}"

我也在学习芹菜,两天前开始学习。我的经验是,在将芹菜集成到web应用程序之前,您需要独立部署芹菜

参观芹菜后,我成功地开始了芹菜申请


从最简单的演示开始。

你能仔细检查一下你是否像《入门指南》中提到的那样在
\uuuu init\uuuuuuuuuy.py
文件中导入了芹菜应用程序吗

我遇到了一个类似的问题,其他一切看起来都是正确的,但当我使用
.delay()

添加导入立即解决了问题。

这可能有助于:

您必须更新project conf文件夹init.py文件:

from __future__ import absolute_import

# This will make sure the app is always imported when
# Django starts so that shared_task will use this app.
from .celery import app as celery_app  

您确定任务存储在代理中吗?是的,使用redis您确定在redis中实际创建了密钥吗?使用
redis cli键“*”进行检查
@Lazykiddy返回了一个空列表。那么您确定与redis的连接正常吗?每次调用
某个任务.delay()
时,它都应该在redis中存储一些内容,这些内容应该由调用您定义的任务的处理程序读取。检查如何调试这个。