Django芹菜不能得到delay()函数

时间:2018-05-29 18:23:56

标签: python django celery

在我的django应用程序中安装celery后,我在Django shell中运行以下命令:

import os
from celery import Celery
from django.apps import apps

# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'ns.settings')

app = Celery('')
app.config_from_object('django.conf:settings')
@app.task                            
def add(x, y):   
   return x + y
add.delay(8, 8)

然后,我发现它永远不会在delay函数中继续运行。

我中止命令后,消息是

/usr/local/lib/python2.7/site-packages/celery/app/task.pyc in delay(self, *args, **kwargs)
    411             celery.result.AsyncResult: Future promise.
    412         """
--> 413         return self.apply_async(args, kwargs)
    414 
    415     def apply_async(self, args=None, kwargs=None, task_id=None, producer=None,

/usr/local/lib/python2.7/site-packages/celery/app/task.pyc in apply_async(self, args, kwargs, task_id, producer, link, link_error, shadow, **options)
    534             link=link, link_error=link_error, result_cls=self.AsyncResult,
    535             shadow=shadow, task_type=self,
--> 536             **options
    537         )
    538 

/usr/local/lib/python2.7/site-packages/celery/app/base.pyc in send_task(self, name, args, kwargs, countdown, eta, task_id, producer, connection, router, result_cls, expires, publisher, link, link_error, add_to_parent, group_id, retries, chord, reply_to, time_limit, soft_time_limit, root_id, parent_id, route_name, shadow, chain, task_type, **options)
    735             with P.connection._reraise_as_library_errors():
    736                 self.backend.on_task_call(P, task_id)
--> 737                 amqp.send_task_message(P, name, message, **options)
    738         result = (result_cls or self.AsyncResult)(task_id)
    739         if add_to_parent:

/usr/local/lib/python2.7/site-packages/celery/app/amqp.pyc in send_task_message(producer, name, message, exchange, routing_key, queue, event_dispatcher, retry, retry_policy, serializer, delivery_mode, compression, declare, headers, exchange_type, **kwargs)
    552                 delivery_mode=delivery_mode, declare=declare,
    553                 headers=headers2,
--> 554                 **properties
    555             )
    556             if after_receivers:

/usr/local/lib/python2.7/site-packages/kombu/messaging.pyc in publish(self, body, routing_key, delivery_mode, mandatory, immediate, priority, content_type, content_encoding, serializer, headers, compression, exchange, retry, retry_policy, declare, expiration, **properties)
    179             body, priority, content_type, content_encoding,
    180             headers, properties, routing_key, mandatory, immediate,
--> 181             exchange_name, declare,
    182         )
    183 

/usr/local/lib/python2.7/site-packages/kombu/connection.pyc in _ensured(*args, **kwargs)
    492                 for retries in count(0):  # for infinity
    493                     try:
--> 494                         return fun(*args, **kwargs)
    495                     except conn_errors as exc:
    496                         if got_connection and not has_modern_errors:

/usr/local/lib/python2.7/site-packages/kombu/messaging.pyc in _publish(self, body, priority, content_type, content_encoding, headers, properties, routing_key, mandatory, immediate, exchange, declare)
    185                  headers, properties, routing_key, mandatory,
    186                  immediate, exchange, declare):
--> 187         channel = self.channel
    188         message = channel.prepare_message(
    189             body, priority, content_type,

/usr/local/lib/python2.7/site-packages/kombu/messaging.pyc in _get_channel(self)
    207         channel = self._channel
    208         if isinstance(channel, ChannelPromise):
--> 209             channel = self._channel = channel()
    210             self.exchange.revive(channel)
    211             if self.on_return:

/usr/local/lib/python2.7/site-packages/kombu/utils/functional.pyc in __call__(self)
     36             return self.__value__
     37         except AttributeError:
---> 38             value = self.__value__ = self.__contract__()
     39             return value
     40 

/usr/local/lib/python2.7/site-packages/kombu/messaging.pyc in <lambda>()
    222             connection = channel
    223             self.__connection__ = connection
--> 224             channel = ChannelPromise(lambda: connection.default_channel)
    225         if isinstance(channel, ChannelPromise):
    226             self._channel = channel

/usr/local/lib/python2.7/site-packages/kombu/connection.pyc in default_channel(self)
    817         """
    818         # make sure we're still connected, and if not refresh.
--> 819         self.ensure_connection()
    820         if self._default_channel is None:
    821             self._default_channel = self.channel()

/usr/local/lib/python2.7/site-packages/kombu/connection.pyc in ensure_connection(self, errback, max_retries, interval_start, interval_step, interval_max, callback, reraise_as_library_errors)
    403                             (), {}, on_error, max_retries,
    404                             interval_start, interval_step, interval_max,
--> 405                             callback)
    406         return self
    407 

/usr/local/lib/python2.7/site-packages/kombu/utils/functional.pyc in retry_over_time(fun, catch, args, kwargs, errback, max_retries, interval_start, interval_step, interval_max, callback)
    343                     if callback:
    344                         callback()
--> 345                     sleep(1.0)
    346                 # sleep remainder after int truncation above.
    347                 sleep(abs(int(tts) - tts))

KeyboardInterrupt: 

我是芹菜的新手,真的不知道是什么让这种情况发生。你能给我一些线索吗?

我的芹菜是4.1.0,海带是4.1.0。

芹菜的现状:

tasks_1      |  -------------- celery@31a95a569866 v4.1.0 (latentcall)
tasks_1      | ---- **** ----- 
tasks_1      | --- * ***  * -- Linux-4.9.87-linuxkit-aufs-x86_64-with-debian-8.10 2018-05-29 15:36:15
tasks_1      | -- * - **** --- 
tasks_1      | - ** ---------- [config]
tasks_1      | - ** ---------- .> app:         __main__:0x7f6a91dbd810
tasks_1      | - ** ---------- .> transport:   amqp://guest:**@localhost:5672//
tasks_1      | - ** ---------- .> results:     disabled://
tasks_1      | - *** --- * --- .> concurrency: 2 (prefork)
tasks_1      | -- ******* ---- .> task events: ON
tasks_1      | --- ***** ----- 
tasks_1      |  -------------- [queues]
tasks_1      |                 .> default          exchange=default(direct) key=default

2 个答案:

答案 0 :(得分:1)

您必须更新项目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  

答案 1 :(得分:0)

很可能,您的代理没有运行,因此Celery无法将消息放入队列中。您能否仔细检查RabbitMQ / Redis(或您在Celery配置中定义的其他内容)是否正在运行?

在Ubuntu上运行RabbitMQ(似乎是默认代理)的示例命令:

sudo service rabbitmq-server start