Django芹菜减少时间,5小时完成1000个任务

时间:2013-03-18 14:38:36

标签: django django-celery

我正在开发环境中运行,所以这在生产中可能有所不同,但是当我从Django Celery运行任务时,它似乎每隔10-20秒才从代理中获取任务。我只是在这一点上进行测试但是假设我发送大约1000个任务,这意味着它需要花费5个多小时才能完成。

这是正常的吗?应该更快吗?或者我做错了什么?

这是我的任务

class SendMessage(Task):
    name = "Sending SMS"
    max_retries = 10
    default_retry_delay = 3

    def run(self, message_id, gateway_id=None, **kwargs):
        logging.debug("About to send a message.")

        # Because we don't always have control over transactions
        # in our calling code, we will retry up to 10 times, every 3
        # seconds, in order to try to allow for the commit to the database
        # to finish. That gives the server 30 seconds to write all of
        # the data to the database, and finish the view.
        try:
            message = Message.objects.get(pk=message_id)
        except Exception as exc:
            raise SendMessage.retry(exc=exc)

        if not gateway_id:
            if hasattr(message.billee, 'sms_gateway'):
                gateway = message.billee.sms_gateway
            else:
                gateway = Gateway.objects.all()[0]
        else:
            gateway = Gateway.objects.get(pk=gateway_id)

        #response = gateway._send(message)
        print(message_id)

        logging.debug("Done sending message.")

从我的视图中运行

 for e in Contact.objects.filter(contact_owner=request.user etc etc):
            SendMessage.delay(e.id, message)

1 个答案:

答案 0 :(得分:5)

是的,这是正常的。这是要使用的默认工作程序。他们设置此默认值,以便它不会影响应用程序的性能。

还有另一种方法可以改变它。任务装饰器可以采用许多选项来改变任务的行为方式。传递给任务装饰器的任何关键字参数实际上都将被设置为生成的任务类的属性。

您可以设置速率限制,以限制在给定时间范围内可以运行的任务数。

//means hundred tasks a minute, another /s (second) and /h (hour)
CELERY_DEFAULT_RATE_LIMIT = "100/m" --> set in settings