简体   繁体   English

Django Celery socket.error [Errno 61]连接被拒绝

[英]Django Celery socket.error [Errno 61] Connection refused

I'm running Redis, Celery 4.0 and Django 1.10 but receive a [Errrno 61] connection refused error when running task 'test' from shell. 我正在运行Redis,Celery 4.0和Django 1.10,但是从shell运行任务“测试”时收到[Errrno 61]连接被拒绝的错误。 This is my project structure: 这是我的项目结构:

myproj
│
├── app1
    ├── __init__.py
    ├── tasks.py
    myproj
    ├── __init__.py
    ├── urls.py
    ├── settings
    │   ├── __init__.py
    │   ├── base.py
    │   ├── local.py
    ├── local
    │   ├── __init__.py
    │   ├── celery.py
    │   ├── wsgi.py

myproj/app1/tasks.py: myproj / app1 / tasks.py:

from __future__ import absolute_import
from celery import task

@task(name='app1.tasks.test')
def test():
    print('this is a test')

myproj/myproj/local/celery.py: myproj / myproj / local / celery.py:

from __future__ import absolute_import
import os
from celery import Celery

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproj.settings.local')
app = Celery('myproj_local')

app.config_from_object('django.conf:settings')
app.autodiscover_tasks()

myproj/myproj/local/ __init__.py : myproj / myproj / local / __init__.py

from __future__ import absolute_import, unicode_literals
from .celery import app as celery_app
__all__ = ['celery_app']

I think something is wrong in this init file because the task runs from shell when I move the content to myproj/myproj/ __init__.py : 我认为此初始化文件中有问题,因为当我将内容移至myproj / myproj / __init__.py时,任务从外壳运行:

from __future__ import absolute_import, unicode_literals

from .local.celery import app as celery_app

__all__ = ['celery_app']

Celery is running in the myproj directory with command: Celery使用以下命令在myproj目录中运行:

celery -A myproj.local.celery worker -l info

The full error: 完整错误:

python manage.py shell --settings=myproj.settings.local
(InteractiveConsole)
>>> from app1.tasks import test
>>> test.delay()
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File env/lib/python2.7/site-packages/celery/app/task.py", line 413, in delay
return self.apply_async(args, kwargs)
  File env/lib/python2.7/site-packages/celery/app/task.py", line 536, in apply_async
**options
  File env/lib/python2.7/site-packages/celery/app/base.py", line 717, in send_task
amqp.send_task_message(P, name, message, **options)
  File env/lib/python2.7/site-packages/celery/app/amqp.py", line 554, in send_task_message
**properties
  File env/lib/python2.7/site-packages/kombu/messaging.py", line 178, in publish
exchange_name, declare,
 File env/lib/python2.7/site-packages/kombu/connection.py", line 527, in _ensured
errback and errback(exc, 0)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/contextlib.py", line 35, in __exit__
self.gen.throw(type, value, traceback)
  File env/lib/python2.7/site-packages/kombu/connection.py", line 419, in _reraise_as_library_errors
sys.exc_info()[2])
  File env/lib/python2.7/site-packages/kombu/connection.py", line 414, in _reraise_as_library_errors
yield
  File env/lib/python2.7/site-packages/kombu/connection.py", line 515, in _ensured
reraise_as_library_errors=False,
  File env/lib/python2.7/site-packages/kombu/connection.py", line 405, in ensure_connection
callback)
  File env/lib/python2.7/site-packages/kombu/utils/functional.py", line 333, in retry_over_time
return fun(*args, **kwargs)
  File env/lib/python2.7/site-packages/kombu/connection.py", line 261, in connect
return self.connection
  File env/lib/python2.7/site-packages/kombu/connection.py", line 802, in connection
self._connection = self._establish_connection()
  File env/lib/python2.7/site-packages/kombu/connection.py", line 757, in _establish_connection
conn = self.transport.establish_connection()
  File env/lib/python2.7/site-packages/kombu/transport/pyamqp.py", line 130, in establish_connection
conn.connect()
  File env/lib/python2.7/site-packages/amqp/connection.py", line 294, in connect
self.transport.connect()
  File env/lib/python2.7/site-packages/amqp/transport.py", line 103, in connect
self._connect(self.host, self.port, self.connect_timeout)
  File env/lib/python2.7/site-packages/amqp/transport.py", line 144, in _connect
self.sock.connect(sa)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 228, in meth
return getattr(self._sock,name)(*args)
OperationalError: [Errno 61] Connection refused

Yeah! 是的 I got it! 我知道了! I had same problem. 我有同样的问题。 In file myproj/app1/tasks.py: 在文件myproj / app1 / tasks.py中:

from __future__ import absolute_import
from celery import task
app = Celery('myproj_local')
app.config_from_object('django.conf:settings')
@app.task(name='random_name')
def test():
    print('this is a test')

Then, celery will be initialized in "app", and config will be loaded in "app", and your task will be delayed. 然后,芹菜将在“ app”中初始化,并且配置将在“ app”中加载,并且您的任务将被延迟。

But it will work only if your config is valid. 但是,只有在您的配置有效的情况下,它才有效。

You need to set the BROKER_URL to point to REDIS. 您需要将BROKER_URL设置为指向REDIS。

If you have one queue only, make sure your worker is connected to default queue. 如果只有一个队列,请确保您的工作程序已连接到默认队列。

If you specified default queue in your settings, you need to set your worker to pickup task as below: 如果您在设置中指定了默认队列,则需要将您的工作程序设置为如下所示的代答任务:

celery -A myproj.local.celery worker -l info -Q queue_name

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM