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

pnwntuvh  于 2024-01-06  发布在  Go
关注(0)|答案(4)|浏览(167)

我正在使用《Django 2 by Example》一书中的代码示例来构建我的电子商务商店。视图正常工作,但任务从未执行。没有错误。页面在“等待本地主机..."时冻结。我在代码中做错了什么?

views.py

  1. import ...
  2. from .tasks import order_created
  3. def order_create(request):
  4. cart = Cart(request)
  5. if request.method == 'POST':
  6. form = OrderCreateForm(request.POST)
  7. if form.is_valid():
  8. order = form.save(commit=False)
  9. if cart.promo_code:
  10. order.promo_code = cart.promo_code
  11. order.discount = cart.promo_code.discount
  12. order.save()
  13. for item in cart:
  14. OrderItem.objects.create(order=order,
  15. book=item['book'],
  16. price=item['price'],
  17. quantity=item['quantity'])
  18. # clear the cart
  19. cart.clear()
  20. print("clean cart")
  21. # set the order in the session
  22. request.session['order_id'] = order.id
  23. print("id - check")
  24. # launch asynchronous task
  25. order_created.delay(order.id) # here I stuck
  26. print("clean cart")
  27. # redirect for payment
  28. return redirect(reverse('payment:process'))
  29. else:
  30. form = OrderCreateForm()
  31. return render(request,
  32. 'orders/order/create.html',
  33. {'cart': cart, 'form': form})

字符串

tasks.py

  1. from celery import task
  2. from django.core.mail import send_mail
  3. from .models import Order
  4. @task
  5. def order_created(order_id):
  6. """
  7. Task to send an e-mail notification when an order is
  8. successfully created.
  9. """
  10. order = Order.objects.get(id=order_id)
  11. subject = 'Order nr. {}'.format(order.id)
  12. message = 'Dear {},\n\nYou have successfully placed an order.\
  13. Your order id is {}.'.format(order.first_name,
  14. order.id)
  15. mail_sent = send_mail(subject,
  16. message,
  17. '[email protected]',
  18. [order.email])
  19. return mail_sent


我用的是celery==4.2.0Django==2.0.7 Ubuntu 18.04

已删除:celery.py中添加了这两行代码。冻结消失了,订单页面现在重定向我进行进一步的付款流程。但任务order_created仍然没有运行。

  1. app.conf.broker_url = 'redis://localhost:6379/1'
  2. # app.conf.broker_transport_options = {'visibility_timeout': 3600} # 1 hour.
  3. app.config_from_object('django.conf:settings', namespace='CELERY')

$redis-monitor

输出如下:

  1. OK
  2. 1531137225.134559 [1 127.0.0.1:48116] "ZREVRANGE" "book:1:purchased_with" "0" "-1"
  3. 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\"}}"
  4. 1531137464.423873 [0 127.0.0.1:48192] "SELECT" "1"
  5. 1531137464.424085 [1 127.0.0.1:48192] "ZREVRANGE" "book:1:purchased_with" "0" "-1"
  6. 1531137479.993181 [0 127.0.0.1:48198] "SELECT" "1"
  7. 1531137479.993295 [1 127.0.0.1:48198] "PING"
  8. 1531137479.994161 [0 127.0.0.1:48200] "SELECT" "1"
  9. 1531137479.994260 [1 127.0.0.1:48200] "MULTI"
  10. 1531137479.994279 [1 127.0.0.1:48200] "LLEN" "celery"
  11. 1531137479.994286 [1 127.0.0.1:48200] "LLEN" "celery\x06\x163"
  12. 1531137479.994291 [1 127.0.0.1:48200] "LLEN" "celery\x06\x166"
  13. 1531137479.994295 [1 127.0.0.1:48200] "LLEN" "celery\x06\x169"
  14. 1531137479.994299 [1 127.0.0.1:48200] "EXEC"
  15. 1531137479.994618 [1 127.0.0.1:48200] "SADD" "_kombu.binding.celery" "celery\x06\x16\x06\x16celery"
  16. 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\"}}"
  17. 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\"}}"

u5i3ibmn

u5i3ibmn1#

你能仔细检查一下你在__init__.py文件中导入了Celery应用程序吗?https://docs.celeryproject.org/en/latest/django/first-steps-with-django.html
我遇到了类似的问题,其他一切看起来都是正确的,但当我使用.delay()时,任务挂起了
添加导入立即修复了问题。

xhv8bpkk

xhv8bpkk2#

也许这会有所帮助:
你必须更新项目conf文件夹init.py文件:

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

字符串

zpgglvta

zpgglvta3#

我也在学习celery,两天前开始学习。我的经验是,在将celery集成到Web应用程序之前,您需要独立部署好celery。
在访问celery 文档页面后,我成功地启动了我的celery 应用程序。
从最简单的演示开始。

tvokkenx

tvokkenx4#

在Windows和celery4.0+上有一个小的不兼容性,解决这个问题:
1 -安装gevent:
pip install gevent
2 -运行celery :
西芹-一<proj_name>个工人-l信息-P gevent

相关问题