我正在尝试按照http://docs.celeryproject.org/en/latest/django/first-steps-with-django.html的指示,让芹菜与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- packages / django_celery-3.0.9-py2.7.egg / djcelery / loaders.py:124:UserWarning:Using 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:
的摘录# 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',
)
有什么想法吗?
答案 0 :(得分:1)
尝试添加
import djcelery
djcelery.setup_loader()
在您的经纪人和后端配置之后,同样根据https://devcenter.heroku.com/articles/django#running-a-worker指南,您需要更改INSTALLED_APPS订单
'kombu.transport.django',
'djcelery'
答案 1 :(得分:1)
问题似乎是,当从交互式shell排队任务时,指令会给你一个未配置的加载器。
如果您遇到传输问题,则在运行芹菜服务器时会收到错误,而不是在排队任务时收到错误。
这对我有用:
>>> 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
答案 2 :(得分:0)
以前的答案应该可以正常工作。但是我在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'