dkh*_*upt 6 python django redis celery celery-task
更新:为了简单起见,我决定尝试使用 Django 作为代理,因为我假设我在 Redis 设置中做错了什么。但是,在进行文档中描述的更改后,当尝试使用.delay(). Celery 工作线程启动并显示它已连接到 Django 以进行传输。这可能是防火墙问题吗?
原来的
\n我正在开发一个 Django 项目并尝试添加后台任务。我已经安装了 Celery 并选择了 Redis 作为代理,并且也安装了它(我在 Windows 机器上,仅供参考)。celery Worker 启动,连接到 Redis 服务器,并发现我的shared_tasks
-------------- celery@GALACTICA v3.1.19 (Cipater)\n---- **** -----\n--- * *** * -- Windows-7-6.1.7601-SP1\n-- * - **** ---\n- ** ---------- [config]\n- ** ---------- .> app: proj:0x2dbf970\n- ** ---------- .> transport: redis://localhost:6379/0\n- ** ---------- .> results: disabled\n- *** --- * --- .> concurrency: 8 (prefork)\n-- ******* ----\n--- ***** ----- [queues]\n -------------- .> celery exchange=celery(direct) key=celery\n\n\n[tasks]\n . app.tasks.add\n . app.tasks.mul\n . app.tasks.xsum\n . proj.celery.debug_task\n\n[2016-01-16 11:53:05,586: INFO/MainProcess] Connected to redis://localhost:6379/\n0\n[2016-01-16 11:53:06,611: INFO/MainProcess] mingle: searching for neighbors\n[2016-01-16 11:53:09,628: INFO/MainProcess] mingle: all alone\nc:\\python34\\lib\\site-packages\\celery\\fixups\\django.py:265: UserWarning: Using se\nttings.DEBUG leads to a memory leak, never use this setting in production enviro\nnments!\n warnings.warn(\'Using settings.DEBUG leads to a memory leak, never \'\n\n[2016-01-16 11:53:14,670: WARNING/MainProcess] c:\\python34\\lib\\site-packages\\cel\nery\\fixups\\django.py:265: UserWarning: Using settings.DEBUG leads to a memory le\nak, never use this setting in production environments! warnings.warn(\'Using settings.DEBUG leads to a memory leak, never \'\n\n[2016-01-16 11:53:14,671: WARNING/MainProcess] celery@GALACTICA ready.\nRun Code Online (Sandbox Code Playgroud)\n我正在关注介绍文档,因此任务非常简单,包括一个名为add. 我可以在 python shell 中自行运行任务,但是当我尝试调用add.delay()让 celery 处理它时,连接似乎不成功:
>>> add.delay(2,2)\nTraceback (most recent call last):\nFile "C:\\Python34\\lib\\site-packages\\kombu\\utils\\__init__.py", line 423, in __call__\nreturn self.__value__\nAttributeError: \'ChannelPromise\' object has no attribute \'__value__\'\n\nDuring handling of the above exception, another exception occurred:\n\nTraceback (most recent call last):\nFile "C:\\Python34\\lib\\site-packages\\kombu\\connection.py", line 436, in _ensured\nreturn fun(*args, **kwargs)\nFile "C:\\Python34\\lib\\site-packages\\kombu\\messaging.py", line 177, in _publish\nchannel = self.channel\nFile "C:\\Python34\\lib\\site-packages\\kombu\\messaging.py", line 194, in _get_channel\nchannel = self._channel = channel()\nFile "C:\\Python34\\lib\\site-packages\\kombu\\utils\\__init__.py", line 425, in __call__\nvalue = self.__value__ = self.__contract__()\nFile "C:\\Python34\\lib\\site-packages\\kombu\\messaging.py", line 209, in <lambda>\nchannel = ChannelPromise(lambda: connection.default_channel) File "C:\\Python34\\lib\\site-packages\\kombu\\connection.py", line 756, in default_channel\nself.connection\nFile "C:\\Python34\\lib\\site-packages\\kombu\\connection.py", line 741, in connection\nself._connection = self._establish_connection()\nFile "C:\\Python34\\lib\\site-packages\\kombu\\connection.py", line 696, in _establish_connection\nconn = self.transport.establish_connection()\nFile "C:\\Python34\\lib\\site-packages\\kombu\\transport\\pyamqp.py", line 116, in establish_connection\nconn = self.Connection(**opts)\nFile "C:\\Python34\\lib\\site-packages\\amqp\\connection.py", line 165, in __init__\nself.transport = self.Transport(host, connect_timeout, ssl)\nFile "C:\\Python34\\lib\\site-packages\\amqp\\connection.py", line 186, in Transport\nreturn create_transport(host, connect_timeout, ssl)\nFile "C:\\Python34\\lib\\site-packages\\amqp\\transport.py", line 299, in create_transport\nreturn TCPTransport(host, connect_timeout)\nFile "C:\\Python34\\lib\\site-packages\\amqp\\transport.py", line 95, in __init__\nraise socket.error(last_err)\nOSError: [WinError 10061] No connection could be made because the target machine actively refused it\n\nDuring handling of the above exception, another exception occurred:\n\nTraceback (most recent call last):\nFile "<stdin>", line 1, in <module>\nFile "C:\\Python34\\lib\\site-packages\\celery\\app\\task.py", line 453, in delay\nreturn self.apply_async(args, kwargs)\nFile "C:\\Python34\\lib\\site-packages\\celery\\app\\task.py", line 560, in apply_async\n**dict(self._get_exec_options(), **options)\nFile "C:\\Python34\\lib\\site-packages\\celery\\app\\base.py", line 354, in send_task\nreply_to=reply_to or self.oid, **options\nFile "C:\\Python34\\lib\\site-packages\\celery\\app\\amqp.py", line 305, in publish_task\n**kwargs\nFile "C:\\Python34\\lib\\site-packages\\kombu\\messaging.py", line 172, in publish\nrouting_key, mandatory, immediate, exchange, declare)\nFile "C:\\Python34\\lib\\site-packages\\kombu\\connection.py", line 457, in _ensured\ninterval_max)\nFile "C:\\Python34\\lib\\site-packages\\kombu\\connection.py", line 369, in ensure_connection\ninterval_start, interval_step, interval_max, callback)\nFile "C:\\Python34\\lib\\site-packages\\kombu\\utils\\__init__.py", line 246, in retry_over_time\nreturn fun(*args, **kwargs)\nFile "C:\\Python34\\lib\\site-packages\\kombu\\connection.py", line 237, in connect\nreturn self.connection\nFile "C:\\Python34\\lib\\site-packages\\kombu\\connection.py", line 741, in connection\nself._connection = self._establish_connection()\nFile "C:\\Python34\\lib\\site-packages\\kombu\\connection.py", line 696, in _establish_connection\nconn = self.transport.establish_connection()\nFile "C:\\Python34\\lib\\site-packages\\kombu\\transport\\pyamqp.py", line 116, in establish_connection\nconn = self.Connection(**opts)\nFile "C:\\Python34\\lib\\site-packages\\amqp\\connection.py", line 165, in __init__\nself.transport = self.Transport(host, connect_timeout, ssl)\nFile "C:\\Python34\\lib\\site-packages\\amqp\\connection.py", line 186, in Transport\nreturn create_transport(host, connect_timeout, ssl)\nFile "C:\\Python34\\lib\\site-packages\\amqp\\transport.py", line 299, in create_transport\nreturn TCPTransport(host, connect_timeout)\nFile "C:\\Python34\\lib\\site-packages\\amqp\\transport.py", line 95, in __init__\nraise socket.error(last_err)\nOSError: [WinError 10061] No connection could be made because the target machine actively refused it\nRun Code Online (Sandbox Code Playgroud)\n芹菜工作线程运行时,控制台上没有输出,所以我认为它永远不会获得任务。我相信我的settings.py、celery.py和tasks.py没问题:
\n设置.py:
\n#celery settings\nBROKER_URL = \'redis://localhost:6379/0\'\nRun Code Online (Sandbox Code Playgroud)\n芹菜.py:
\nfrom __future__ import absolute_import\n\nimport os\n\nfrom celery import Celery\n\n# set the default Django settings module for the \'celery\' program.\nos.environ.setdefault(\'DJANGO_SETTINGS_MODULE\', \'proj.settings\')\n\nfrom django.conf import settings # noqa\n\napp = Celery(\'proj\')\n\n# Using a string here means the worker will not have to\n# pickle the object when using Windows.\napp.config_from_object(\'django.conf:settings\')\napp.autodiscover_tasks(lambda: settings.INSTALLED_APPS)\n\n\n@app.task(bind=True)\ndef debug_task(self):\n print(\'Request: {0!r}\'.format(self.request))\nRun Code Online (Sandbox Code Playgroud)\n任务.py:
\nfrom __future__ import absolute_import\n\n#from proj.celery import app\nfrom celery import shared_task\n\n\n@shared_task\ndef add(x, y):\n return x + y\n\n\n@shared_task\ndef mul(x, y):\n return x * y\n\n\n@shared_task\ndef xsum(numbers):\n return sum(numbers)\nRun Code Online (Sandbox Code Playgroud)\n我的项目布局几乎与 GitHub 上的 Celery 示例 Django 项目布局以及此处的示例相同示例相同。看起来像:
\nproj\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 proj\n\xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 celery.py \n\xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 __init__.py \n\xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 settings.py \n\xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 urls.py\n\xe2\x94\x82 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 wsgi.py\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 manage.py\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 app\n \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 __init__.py\n \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 models.py\n \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 tasks.py \n \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 tests.py\n \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 views.py\nRun Code Online (Sandbox Code Playgroud)\n对我的项目中另一个名为“app”的应用程序表示歉意 - 它使阅读起来有点混乱,并且是在安装了 PTVS 的 Visual Studio 中自动生成基础项目的结果。我可能早就可以改变它,但我没有意识到这个名字是如此模糊。
\n感谢您的任何想法 - 我已经被这个问题难住了一段时间。
\n小智 8
在互联网上滚动后,我遇到了同样的错误,我没有解决方案,因为我忘记添加以下代码:
from .celery import app as celery_app
__all__ = ('celery_app',)
Run Code Online (Sandbox Code Playgroud)
到我的项目目录的 __init.py 文件解决了我的错误
我解决了这个问题,但我不知道如何解决。第二天我又回到了这个确切的配置,任务正在交给芹菜工人。
也许我重新启动的服务之一是关键,但我不确定。
如果其他人遇到这种情况,尤其是在 Windows 上:请确保您的 redis 服务器处于活动状态,并且您可以看到来自 ping 的传入连接以及任务。在发布这个问题之前我已经这样做了,但它似乎可能是配置错误的候选者。
| 归档时间: |
|
| 查看次数: |
6062 次 |
| 最近记录: |