django 数据库作为芹菜经纪人提供连接被拒绝错误

Posted

技术标签:

【中文标题】django 数据库作为芹菜经纪人提供连接被拒绝错误【英文标题】:django database as a celery broker giving connection refused error 【发布时间】:2012-08-31 21:25:40 【问题描述】:

我正在尝试按照http://docs.celeryproject.org/en/latest/django/first-steps-with-django.html 上的说明使 celery 与 django 一起使用。

至于经纪人,我正在尝试按照http://docs.celeryproject.org/en/latest/getting-started/brokers/django.html#broker-django 上的说明设置 django 数据库。

我可以成功启动一个工人,如下所示:

*****-MacBook-Air:celeryproject *****$ python manage.py celery worker -E --loglevel=info /Library/Python/2.7/site-packages/django_celery-3.0.9-py2.7.egg/djcelery/loaders.py:124:
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 leak, never "    --------------
celery@*****-MacBook-Air.local v3.0.9 (Chiastic Slide)  
---- **** -----   
--- * ***  * -- [Configuration]  
-- * - **** --- . broker:      django://localhost//  
- ** ---------- . app:         default:0x10b785fd0 (djcelery.loaders.DjangoLoader)  
- ** ---------- . concurrency: 4 (processes)  
- ** ---------- . events:      ON  
- ** ----------   
- *** --- * --- [Queues]   
-- ******* ---- . celery:      exchange:celery(direct) binding:celery  
--- ***** -----   

[Tasks]     . celeryapp.tasks.add  

[2012-09-06 20:03:23,711: WARNING/MainProcess] celery@*****-MacBook-Air.local has started.
[2012-09-06 20:03:24,406: WARNING/PoolWorker-3] /Library/Python/2.7/site- 
packages/django_celery-3.0.9-py2.7.egg/djcelery/loaders.py:124: 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 leak, never "
[2012-09-06 20:03:24,406: WARNING/PoolWorker-1] /Library/Python/2.7/site-packages/django_celery-3.0.9-py2.7.egg/djcelery/loaders.py:124: 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 leak, never "
[2012-09-06 20:03:24,406: WARNING/PoolWorker-2] /Library/Python/2.7/site-packages/django_celery-3.0.9-py2.7.egg/djcelery/loaders.py:124: 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 leak, never "

[2012-09-06 20:03:24,406:警告/PoolWorker-4] /Library/Python/2.7/site- 包/django_celery-3.0.9-py2.7.egg/djcelery/loaders.py:124:用户警告:使用 settings.DEBUG 会导致内存泄漏,千万不要在生产环境中使用这个设置! warnings.warn("使用 settings.DEBUG 会导致内存泄漏,绝不会"

但是当我继续按照说明并调用在入门步骤中创建的添加任务时,我遇到了以下错误:

Python 2.7.1 (r271:86832, Jul 31 2011, 19:30:53)   
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin  
Type "help", "copyright", "credits" or "license" for more information.  
>>> from celeryapp.tasks import add  
>>> add.delay(2,2)  
Traceback (most recent call last):  
  File "<stdin>", line 1, in <module>  
  File "/Library/Python/2.7/site-packages/celery-3.0.9-py2.7.egg/celery/app/task.py", line   343, in delay  
    return self.apply_async(args, kwargs)  
  File "/Library/Python/2.7/site-packages/celery-3.0.9-py2.7.egg/celery/app/task.py", line   458, in apply_async  
    with app.producer_or_acquire(producer) as P:  
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/contextlib.py", line 17, in __enter__  
    return self.gen.next()  
  File "/Library/Python/2.7/site-packages/celery-3.0.9-py2.7.egg/celery/app/base.py", line 256, in producer_or_acquire  
    with self.amqp.producer_pool.acquire(block=True) as producer:  
  File "/Library/Python/2.7/site-packages/kombu-2.4.5-py2.7.egg/kombu/connection.py", line  712, in acquire
    R = self.prepare(R)  
  File "/Library/Python/2.7/site-packages/kombu-2.4.5-py2.7.egg/kombu/pools.py", line 54, in prepare  
    p = p()  
  File "/Library/Python/2.7/site-packages/kombu-2.4.5-py2.7.egg/kombu/pools.py", line 45, in <lambda>  
    return lambda: self.create_producer()  
  File "/Library/Python/2.7/site-packages/kombu-2.4.5-py2.7.egg/kombu/pools.py", line 42, in create_producer  
    return self.Producer(self._acquire_connection())  
  File "/Library/Python/2.7/site-packages/celery-3.0.9-py2.7.egg/celery/app/amqp.py", line 160, in __init__  
    super(TaskProducer, self).__init__(channel, exchange, *args, **kwargs)  
  File "/Library/Python/2.7/site-packages/kombu-2.4.5-py2.7.egg/kombu/messaging.py", line 83, in __init__  
    self.revive(self.channel)  
  File "/Library/Python/2.7/site-packages/kombu-2.4.5-py2.7.egg/kombu/messaging.py", line 174, in revive
    channel = self.channel = maybe_channel(channel)  
  File "/Library/Python/2.7/site-packages/kombu-2.4.5-py2.7.egg/kombu/connection.py", line 886, in maybe_channel  
    return channel.default_channel  
  File "/Library/Python/2.7/site-packages/kombu-2.4.5-py2.7.egg/kombu/connection.py", line 624, in default_channel  
    self.connection  
  File "/Library/Python/2.7/site-packages/kombu-2.4.5-py2.7.egg/kombu/connection.py", line 617, in connection  
    self._connection = self._establish_connection()  
  File "/Library/Python/2.7/site-packages/kombu-2.4.5-py2.7.egg/kombu/connection.py", line 576, in _establish_connection  
    conn = self.transport.establish_connection()  
  File "/Library/Python/2.7/site-packages/kombu-2.4.5-py2.7.egg/kombu/transport/amqplib.py", line 344, in establish_connection  
    connect_timeout=conninfo.connect_timeout)  
  File "/Library/Python/2.7/site-packages/kombu-2.4.5-py2.7.egg/kombu/transport/amqplib.py", line 154, in __init__  
    super(Connection, self).__init__(*args, **kwargs)  
  File "build/bdist.macosx-10.7-intel/egg/amqplib/client_0_8/connection.py", line 129, in __init__  
  File "build/bdist.macosx-10.7-intel/egg/amqplib/client_0_8/transport.py", line 281, in create_transport  
  File "build/bdist.macosx-10.7-intel/egg/amqplib/client_0_8/transport.py", line 85, in __init__  
socket.error: [Errno 61] Connection refused  

基本上,这个应用似乎无法与我的经纪人设置传输。

以下是我的settings.py的sn-p:

# Django settings for celeryproject project.
import djcelery
djcelery.setup_loader()

## Broker settings.
BROKER_URL= 'django://'
BROKER_BACKEND = "djkombu.transport.DatabaseTransport"

# List of modules to import when celery starts.
CELERY_IMPORTS = ("celeryapp.tasks", )

## Using the database to store task state and results.
#CELERY_RESULT_BACKEND = "database"
#CELERY_RESULT_DBURI = "sqlite:///mydatabase.db"

CELERY_ANNOTATIONS = "tasks.add": "rate_limit": "10/s"

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'celeryapp',
    'djcelery',
    #'djcelery.transport',
    'kombu.transport.django',
    # Uncomment the next line to enable the admin:
    # 'django.contrib.admin',
    # Uncomment the next line to enable admin documentation:
    # 'django.contrib.admindocs',
)

有什么想法吗?

【问题讨论】:

我也有同样的问题。使用 celery/django-celery 3.0.11 和 kombu 2.5.4。 【参考方案1】:

尝试添加

import djcelery
djcelery.setup_loader()

在您的代理和后端配置之后,同样根据https://devcenter.heroku.com/articles/django#running-a-worker 指南,mb 您需要更改 INSTALLED_APPS 顺序

'kombu.transport.django',
'djcelery'

【讨论】:

【参考方案2】:

问题似乎在于,当您从交互式 shell 中对任务进行排队时,说明会留下未配置的加载程序。

如果您遇到传输问题,您会在运行 celery 服务器时收到错误,而不是在排队任务时。

这对我有用:

>>> import djcelery
>>> djcelery.setup_loader()
>>> from myapp.tasks import add
>>> add.delay(2,2)
<AsyncResult: d43a673a-c2a5-4116-b60e-c59afa7dff51>
>>> result = add.delay(4,5)
>>> result.ready()
True
>>> result.result
9
>>> result.get()
9
>>> result.successful()
True

【讨论】:

【参考方案3】:

以前的答案应该可以正常工作。但是我在 django 项目中遇到了相同的connection refused 错误,这是因为我忘记将 celery 设置放在 settings.py 中。要解决这个问题,只需在 settings.py 中添加以下设置:

# CELERY SETTINGS
BROKER_URL = 'redis://localhost:6379/0'
CELERY_ACCEPT_CONTENT = ['json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'

【讨论】:

以上是关于django 数据库作为芹菜经纪人提供连接被拒绝错误的主要内容,如果未能解决你的问题,请参考以下文章

Django- [Errno 111] 使用 smtp 时连接被拒绝

芹菜是如何工作的?

芹菜工人的水平尺度导致相同的处理时间

芹菜任务不会在 django 中执行

芹菜不处理来自 RabbitMQ 的任务

Nginx、FastCGI 和 Django 连接被拒绝错误