简体   繁体   English

Celery Flask - 错误:[Errno 111]连接被拒绝

[英]Celery Flask — error: [Errno 111] Connection refused

After running through the basic example for flask-celery(runs fine as far as I can tell) I'm trying to integrate this to my own project. 在完成了烧瓶芹菜的基本示例之后(据我所知,运行正常)我正在尝试将其集成到我自己的项目中。 Basically, I'm using this below: 基本上,我在下面使用这个:

from flask import Blueprint, jsonify, request, session
from flask.views import MethodView
from celery.decorators import task

blueprint = Blueprint('myapi', __name__)

class MyAPI(MethodView):

    def get(self, tag):
        return get_resource.apply_async(tag)

@task(name="get_task")
def get_resource(tag):
    pass

with the same setup as in the example, I'm getting this error: 使用与示例中相同的设置,我收到此错误:

Traceback (most recent call last):
  File "/x/venv/lib/python2.7/site-packages/flask/app.py", line 1518, in __call__
    return self.wsgi_app(environ, start_response)
  File "/x/venv/lib/python2.7/site-packages/flask/app.py", line 1506, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File "/x/venv/lib/python2.7/site-packages/flask/app.py", line 1504, in wsgi_app
    response = self.full_dispatch_request()
  File "/x/venv/lib/python2.7/site-packages/flask/app.py", line 1264, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/x/venv/lib/python2.7/site-packages/flask/app.py", line 1262, in full_dispatch_request
    rv = self.dispatch_request()
  File "/x/venv/lib/python2.7/site-packages/flask/app.py", line 1248, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/x/venv/lib/python2.7/site-packages/flask/views.py", line 84, in view
    return self.dispatch_request(*args, **kwargs)
  File "/x/venv/lib/python2.7/site-packages/flask/views.py", line 151, in dispatch_request
    return meth(*args, **kwargs)
  File "/x/api/modules/document/document.py", line 14, in get
    return get_resource.apply_async(tag)
  File "/x/venv/lib/python2.7/site-packages/celery/app/task/__init__.py", line 449, in apply_async
    publish = publisher or self.app.amqp.publisher_pool.acquire(block=True)
  File "/x/venv/lib/python2.7/site-packages/kombu/connection.py", line 657, in acquire
    R = self.prepare(R)
  File "/x/venv/lib/python2.7/site-packages/kombu/pools.py", line 54, in prepare
    p = p()
  File "/x/venv/lib/python2.7/site-packages/kombu/pools.py", line 45, in <lambda>
    return lambda: self.create_producer()
  File "/x/venv/lib/python2.7/site-packages/celery/app/amqp.py", line 265, in create_producer
    pub = self.app.amqp.TaskPublisher(conn, auto_declare=False)
  File "/x/venv/lib/python2.7/site-packages/celery/app/amqp.py", line 328, in TaskPublisher
    return TaskPublisher(*args, **self.app.merge(defaults, kwargs))
  File "/x/venv/lib/python2.7/site-packages/celery/app/amqp.py", line 158, in __init__
    super(TaskPublisher, self).__init__(*args, **kwargs)
  File "/x/venv/lib/python2.7/site-packages/kombu/compat.py", line 61, in __init__
    super(Publisher, self).__init__(connection, self.exchange, **kwargs)
  File "/x/venv/lib/python2.7/site-packages/kombu/messaging.py", line 79, in __init__
    self.revive(self.channel)
  File "/x/venv/lib/python2.7/site-packages/kombu/messaging.py", line 168, in revive
    channel = channel.default_channel
  File "/x/venv/lib/python2.7/site-packages/kombu/connection.py", line 581, in default_channel
    self.connection
  File "/x/venv/lib/python2.7/site-packages/kombu/connection.py", line 574, in connection
    self._connection = self._establish_connection()
  File "/x/venv/lib/python2.7/site-packages/kombu/connection.py", line 533, in _establish_connection
    conn = self.transport.establish_connection()
  File "/x/venv/lib/python2.7/site-packages/kombu/transport/amqplib.py", line 279, in establish_connection
    connect_timeout=conninfo.connect_timeout)
  File "/x/venv/lib/python2.7/site-packages/kombu/transport/amqplib.py", line 89, in __init__
    super(Connection, self).__init__(*args, **kwargs)
  File "/x/venv/lib/python2.7/site-packages/amqplib/client_0_8/connection.py", line 129, in __init__
    self.transport = create_transport(host, connect_timeout, ssl)
  File "/x/venv/lib/python2.7/site-packages/amqplib/client_0_8/transport.py", line 281, in create_transport
    return TCPTransport(host, connect_timeout)
  File "/x/venv/lib/python2.7/site-packages/amqplib/client_0_8/transport.py", line 85, in __init__
    raise socket.error, msg
error: [Errno 111] Connection refused

-->

I'm using redis, and if I install rabbitmq I get another error, but I do not understand this right now --the broker should be redis but its isn't finding it or what? 我正在使用redis,如果我安装了rabbitmq,我会收到另一个错误,但我现在不明白这个 - 经纪人应该是redis但它找不到它或者什么? Can anyone give me more of a clue what is going on here? 任何人都可以给我更多线索这里发生了什么? Do I need to import something else, etc. The point is, there is very little beyond the bare bones example and this makes no sense to me. 我是否需要导入其他内容等等。重点是,除了裸露的骨头示例之外几乎没有什么,这对我来说毫无意义。

The most I've been able to determine as that in the Api module there is no access to the 'celery' and when it goes to try and put data there when at the app level, the celery there falls into some defaults, which aren't installed because I'm pointing to redis. 我能够确定的最多,因为在Api模块中无法访问'芹菜',当它在应用程序级别尝试将数据放入那里时,那里的芹菜会出现一些默认值,这些都是安装因为我指的是redis。 Just a guess. 只是一个猜测。 I haven't been able to import information into the module, only determined that calling anything 'celery'(for example, output celery.conf) from the app causes an error -- although I could import celery.task. 我无法将信息导入模块,只是确定从应用程序调用任何“celery”(例如,输出celery.conf)会导致错误 - 尽管我可以导入celery.task。

This is the broker config the application is using, direct from the example: 这是应用程序正在使用的代理配置,直接来自示例:

BROKER_URL = 'redis://localhost:6379/0'
CELERY_RESULT_BACKEND = "redis"
CELERY_REDIS_HOST = "localhost"
CELERY_REDIS_PORT = 6379
CELERY_REDIS_DB = 0

EDIT: 编辑:

If you'd like to see a demo: https://github.com/thrisp/flask-celery-example 如果你想看一个演示: https//github.com/thrisp/flask-celery-example

AS it turns out having BROKER_TRANSPORT = 'redis' in your settings is important for whatever is being passed that I'm passing in (for the setup I put forth here and in the git example), I'm not entirely sure why it isn't in the example bits, but is in the ones I've added but it is -- without this it wants to dump everything onto a default ampq queue. 事实证明,在你的设置中使用BROKER_TRANSPORT ='redis'对我传入的任何内容都很重要(对于我在这里和git示例中提出的设置),我不完全确定为什么它不是在示例位中,但是在我添加的那些位中却是 - 如果没有这个,它想要将所有内容转储到默认的ampq队列中。

EDIT2: EDIT2:

Also, this a rather a big deal, using the upcoming version of Celery simplifies 10,000 issues when using it with Flask, making all of this unecesssary. 此外,这是一个相当大的优点,使用即将推出的Celery版本在使用Flask时简化了10,000个问题,使所有这些都成为不可能的。

You must configure redis to bind the localhost. 您必须配置redis以绑定localhost。 In /etc/redis/redis.conf , uncomment the line with /etc/redis/redis.conf ,取消注释该行

bind 127.0.0.1

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

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