django 任务已接收但未执行

wlwcrazw  于 2023-10-21  发布在  Go
关注(0)|答案(5)|浏览(105)

我在学Django。最近我试着用celery 。我面临的问题是任务正在接收,但它们不执行。
settings.py:

# Celery application definition
CELERY_BROKER_URL = 'redis://localhost:6379'
CELERY_RESULT_BACKEND = 'redis://localhost:6379'
CELERY_ACCEPT_CONTENT = ['application/json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
CELERY_TIMEZONE = 'Asia/Tehran'

celery.py:

from __future__ import absolute_import, unicode_literals

import os
from celery import Celery

# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myshop.settings')
app = Celery('myshop')
app.config_from_object('django.conf.settings', namespace='CELERY')
app.autodiscover_tasks()

@app.task(bind=True)
def debug_task(self):
    print('Request: {0!r}'.format(self.request))

tasks.py:

from celery import task, shared_task
from django.core.mail import send_mail
from .models import Order

import logging

# Get an instance of a logger
logger = logging.getLogger(__name__)

@shared_task()
def order_created(order_id):
    logger.error('msg received')
    """
    Task to send an e-mail notification when an order is
    successfully created.
    """

    order = Order.objects.get(id=order_id)

    order = Order.objects.get(id=order_id)
    subject = f'Order nr. {order.id}'
    message = f'Dear {order.first_name},\n\n' \
              f'You have successfully placed an order.' \
              f'Your order ID is {order.id}.'
    mail_sent = send_mail(subject,
                          message,
                          '[email protected]',
                          [order.email])
    logger.error('mail sent')
    return mail_sent

在views.py中运行任务:

# launch asynchronous task
            logger.error('before task')
            order_created.delay(order.id)
            logger.error('after task')

使用celery -A myshop worker -l info运行celery后的cmd日志:

(venv) E:\Workspace\django-shop\myshop>celery -A myshop worker -l info

 -------------- celery@DESKTOP-F7E0RGJ v4.4.2 (cliffs)
--- ***** -----
-- ******* ---- Windows-10-10.0.18362-SP0 2020-06-23 03:37:15
- *** --- * ---
- ** ---------- [config]
- ** ---------- .> app:         myshop:0x4b93df0
- ** ---------- .> transport:   redis://localhost:6379//
- ** ---------- .> results:     redis://localhost:6379/
- *** --- * --- .> concurrency: 8 (prefork)
-- ******* ---- .> task events: OFF (enable -E to monitor tasks in this worker)
--- ***** -----
 -------------- [queues]
                .> celery           exchange=celery(direct) key=celery

[tasks]
  . myshop.celery.debug_task
  . orders.tasks.order_created

[2020-06-23 03:37:15,800: INFO/MainProcess] Connected to redis://localhost:6379//
[2020-06-23 03:37:15,823: INFO/MainProcess] mingle: searching for neighbors
[2020-06-23 03:37:16,320: INFO/SpawnPoolWorker-2] child process 15856 calling self.run()
[2020-06-23 03:37:16,328: INFO/SpawnPoolWorker-1] child process 13464 calling self.run()
[2020-06-23 03:37:16,332: INFO/SpawnPoolWorker-4] child process 13528 calling self.run()
[2020-06-23 03:37:16,337: INFO/SpawnPoolWorker-3] child process 4660 calling self.run()
[2020-06-23 03:37:16,344: INFO/SpawnPoolWorker-8] child process 9040 calling self.run()
[2020-06-23 03:37:16,346: INFO/SpawnPoolWorker-5] child process 17044 calling self.run()
[2020-06-23 03:37:16,358: INFO/SpawnPoolWorker-7] child process 16480 calling self.run()
[2020-06-23 03:37:16,366: INFO/SpawnPoolWorker-6] child process 16876 calling self.run()
[2020-06-23 03:37:16,885: INFO/MainProcess] mingle: all alone
[2020-06-23 03:37:16,913: WARNING/MainProcess] e:\workspace\django-shop\venv\lib\site-packages\celery\fixups\django.py:202: UserWarning: Using settings.DEBUG leads to a memory
            leak, never use this setting in production environments!
  warnings.warn('''Using settings.DEBUG leads to a memory
[2020-06-23 03:37:16,914: INFO/MainProcess] celery@DESKTOP-F7E0RGJ ready.
[2020-06-23 03:37:16,918: INFO/MainProcess] Received task: orders.tasks.order_created[1d366fb3-262b-4d28-92ab-8a7b85f1deff]
[2020-06-23 03:37:16,921: INFO/MainProcess] Received task: orders.tasks.order_created[a1d79ddb-eb2f-4791-9e8a-f1a71bf5f44e]
[2020-06-23 03:37:16,924: INFO/MainProcess] Received task: orders.tasks.order_created[afb84e39-3d10-42d0-ab9e-333917ff66f9]
[2020-06-23 03:37:16,927: INFO/MainProcess] Received task: orders.tasks.order_created[86dc7ad7-66e6-4f3a-9db1-6f2a5cb5791f]
[2020-06-23 03:37:16,931: INFO/MainProcess] Received task: myshop.celery.debug_task[8169ff3e-ceae-478f-87ab-4020bedb8338]
[2020-06-23 03:37:16,935: INFO/MainProcess] Received task: orders.tasks.order_created[e689b21e-78b6-498b-adb4-f64dedbbbac8]
[2020-06-23 03:37:16,938: INFO/MainProcess] Received task: orders.tasks.order_created[92db7aa9-bf16-4be1-9769-a1986ffc6f6d]
[2020-06-23 03:37:16,940: INFO/MainProcess] Received task: orders.tasks.order_created[5c0e9873-2671-49a6-9c53-db6dfdd041ef]
[2020-06-23 03:37:16,943: INFO/MainProcess] Received task: orders.tasks.order_created[8d17faa5-19d7-49ed-8181-5a334423c1bb]
[2020-06-23 03:37:18,420: INFO/MainProcess] Received task: orders.tasks.order_created[4671d8c9-11dd-4c9a-a7b1-d0cfedab5e0a]
[2020-06-23 03:37:18,443: INFO/MainProcess] Received task: orders.tasks.order_created[c8f588c4-6e21-4fc8-b441-b65456207cd6]
[2020-06-23 03:37:18,453: INFO/MainProcess] Received task: orders.tasks.order_created[509fd1ad-eac9-4aab-b1f1-1c6278ebbffc]
[2020-06-23 03:37:19,080: INFO/SpawnPoolWorker-9] child process 10652 calling self.run()
[2020-06-23 03:37:19,095: INFO/SpawnPoolWorker-12] child process 13548 calling self.run()
[2020-06-23 03:37:19,097: INFO/SpawnPoolWorker-11] child process 252 calling self.run()
[2020-06-23 03:37:19,112: INFO/SpawnPoolWorker-13] child process 4604 calling self.run()
[2020-06-23 03:37:19,119: INFO/SpawnPoolWorker-14] child process 12696 calling self.run()
[2020-06-23 03:37:19,121: INFO/SpawnPoolWorker-16] child process 13080 calling self.run()
[2020-06-23 03:37:19,125: INFO/SpawnPoolWorker-10] child process 12420 calling self.run()
[2020-06-23 03:37:19,159: INFO/SpawnPoolWorker-15] child process 4468 calling self.run()
[2020-06-23 03:37:21,177: INFO/SpawnPoolWorker-20] child process 14032 calling self.run()
[2020-06-23 03:37:21,183: INFO/SpawnPoolWorker-19] child process 11220 calling self.run()
[2020-06-23 03:37:21,183: INFO/SpawnPoolWorker-18] child process 1084 calling self.run()
[2020-06-23 03:37:21,191: INFO/SpawnPoolWorker-17] child process 14020 calling self.run()

并在创建新任务后进行日志记录:

[2020-06-23 03:41:32,735: INFO/MainProcess] Received task: orders.tasks.order_created[758b7636-314b-40df-8458-d03bb1ca2ac2]
[2020-06-23 03:41:33,536: INFO/SpawnPoolWorker-22] child process 16052 calling self.run()
[2020-06-23 03:41:33,537: INFO/SpawnPoolWorker-21] child process 9664 calling self.run()
[2020-06-23 03:41:33,538: INFO/SpawnPoolWorker-24] child process 18172 calling self.run()
[2020-06-23 03:41:33,546: INFO/SpawnPoolWorker-26] child process 9704 calling self.run()
[2020-06-23 03:41:33,547: INFO/SpawnPoolWorker-23] child process 10612 calling self.run()
[2020-06-23 03:41:33,547: INFO/SpawnPoolWorker-25] child process 11176 calling self.run()
omvjsjqw

omvjsjqw1#

根据日志,我假设你在Windows上。但是,由于Celery 4.x不再支持Windows。您遇到的问题也在此Celery issue和此SO question中描述。关于在Windows上运行Celery 4.x的一般讨论可以在here中找到。你的问题的解决办法似乎是使用例如。gevent执行池(关于执行池的更多信息可以在here中找到)。使用Django时:

python manage.py celery worker -P gevent
relj7zay

relj7zay2#

Windows不支持celery 4.0+,所以要解决这个问题:-
1.我们必须使用pip安装gevent:-
pip install gevent
1.然后运行celery ,
西芹-一<proj_name>个工人-l信息-P gevent

kuuvgm7e

kuuvgm7e3#

希望这能帮助我也面临这个问题的人,我在最新版本中使用Django以及Celery。事实上,由于celery Windows版本4不受支持,要解决这个问题,您需要另一个类似Gevent的库更多信息on this page.

iaqfqrcu

iaqfqrcu4#

这当然是由于操作系统环境:供寡妇经营celery 工用途:celery -A YOURPROJECTNAME.celery worker --pool=solo -l info

y3bcpkx1

y3bcpkx15#

我也有同样的问题。我认为窗口不允许celery 运行多个线程或工人。我一直都是一个人打台球。当我忘记做那件事的时候,我就有了问题。
运行以下命令:
celery -一个eazzier工人-l信息--池=独奏
而不是:celery -A eazzier worker --loglevel=info

相关问题