【问题标题】:celery .delay freezes for this task but runs for otherscelery .delay 为这个任务冻结,但为其他任务运行
【发布时间】:2020-08-10 15:29:47
【问题描述】:

我正在尝试使用 celery 发送通知。

@shared_task(name='send_notifis')
def send_notifs(device_ids, title, message):
    from pills_reminder.models import UserNotifications, UserDevice
    devices = UserDevice.objects.filter(id__in=device_ids)
    print(devices)
    device_tokens = []
    for device in devices:
        UserNotifications.objects.create(
            uid=device.device_id,
            title=title,
            message=message,
        )
        print(UserNotifications)
        device_tokens.append(device.registration_token)

    if len(device_tokens) > 1:
        device_tokens = ["".join(token.split()) for token in device_tokens]
        response = push_service.notify_multiple_devices(registration_ids=device_tokens,
                                             message_title=title,
                                             message_body=message)
    elif len(device_tokens) == 1:
        registration_id = "".join(device_tokens[0].split())
        response = push_service.notify_single_device(registration_id=registration_id,
                                          message_title=title,
                                          message_body=message)
    else:
        pass
    print(response)
    return True

这在没有 .delay() 和运行时使用 python manage.py shell

>>> send_notifs.delay(devices, title='title', message='message')
<AsyncResult: f54188f8-cec6-42dd-a840-b097abffd7f4>

但是当我使用 Django 模型 post_save 信号调用时它会冻结。

@receiver(post_save, sender=Notification)
def Notification_post_save_handler(sender, instance, **kwargs):
    print('hello from post_save signal')
    devices = instance.get_devices()
    # send_notifs(devices)
    if len(devices)>0:
        send_notifs.delay(devices,
                    title=instance.title,
                    message=instance.message)

上面的代码冻结了执行,但没有.delay。它工作正常。

更新:1

.delay 的上述任务是从 python manage.py shell 而不是从 runserver 运行的。所以问题出在 celery 和 Django 设置上。因此我深入挖掘并发现了

shell 运行时我明白了,

>>> add.app.conf #(add is a async task here)
{'broker_url': 'redis://localhost:6379/1'}, ...

但从runserver 运行会给出:

`{'broker_url': None}`

现在我正在寻找如何正确设置设置?我正在使用带有 celery.py 的 django-configurations

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', 'core.settings')
os.environ.setdefault('DJANGO_CONFIGURATION', 'Development')

import configurations
configurations.setup()

app = Celery('core')
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))

感谢任何帮助。感谢您的时间和耐心。

【问题讨论】:

    标签: django celery celery-task


    【解决方案1】:

    使用这个

    from configurations import importer
    importer.install()
    

    代替

    import configurations
    configurations.setup()
    

    【讨论】:

      【解决方案2】:

      原来在django-configuration docs 中指定的包含芹菜的设置导致了问题。修改了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', 'core.settings')
      os.environ.setdefault('DJANGO_CONFIGURATION', 'Development')
      
      from configurations import importer
      importer.install()
      
      # import configurations
      # configurations.setup()
      
      app = Celery('core')
      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))
      

      【讨论】:

        猜你喜欢
        • 2021-12-20
        • 2017-06-15
        • 1970-01-01
        • 2011-09-15
        • 2019-11-24
        • 1970-01-01
        • 2015-08-17
        • 2015-04-09
        • 1970-01-01
        相关资源
        最近更新 更多