14

我遵循 Celery (Django) 的第一步,并尝试在后台运行繁重的进程。我安装了 RabbitMQ 服务器。但是,当我尝试时, celery -A my_app worker -l info它会引发以下错误

File "<frozen importlib._bootstrap>", line 994, in _gcd_import
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "c:\anaconda3\lib\site-packages\celery\concurrency\prefork.py", line 
18, in <module>
from celery.concurrency.base import BasePool
File "c:\anaconda3\lib\site-packages\celery\concurrency\base.py", line 15, 
in <module>
from celery.utils import timer2
File "c:\anaconda3\lib\site-packages\celery\utils\timer2.py", line 16, in 
<module>
from kombu.asynchronous.timer import Entry
ModuleNotFoundError: No module named 'kombu.asynchronous.timer'

我已经搜索了很多,但似乎无法使其正常工作。任何帮助将不胜感激。谢谢!

4

9 回答 9

8

我在 pip (3.1.26Post2)的默认 Celery 安装中遇到了这个问题。如上所述,我安装了 3.1.25 版本,但 Celery 仍然无法正常工作。因此,我明确安装了最新版本:

点安装芹菜==4.3

现在一切正常!

于 2019-10-04T02:19:55.587 回答
5

django-celery我在阅读celery 4.4 文档时尝试安装后登陆这里,这个包强制 celery 版本3.1.26.post2,所以我不得不:

pip uninstall django-celery
pip uninstall celery && pip install celery # Uninstall 3.1 and install latest

正如文档中明确指出的那样:

现在支持开箱即用的 Django,因此本文档仅包含集成 Celery 和 Django 的基本方法。您将使用与非 Django 用户相同的 API,因此建议您先阅读Celery 的第一步教程,然后再返回本教程。

于 2020-04-12T14:02:33.640 回答
3

TL;DR:从 virtualenv 的根目录中删除 kombu 目录(如果存在)。它可能只在 Windows 上失败。

这似乎是一个怪癖。我发现了同样的错误,我检查了发生了什么。

pip下载的wheel包看起来不错(里面有kombu.asynchronous.timer)。最后一个版本(目前是 4.2.0)的发布也很好。奇怪的是我在 virtualenv 安装中发现的。

我在我的 virtualenv 根目录中找到了一个 kombu 目录,其中包含库的内容,但它也有一个“异步”目录和一个“异步”目录。这些目录不是来自 4.2.0 版本,因为 async 有 timer.py 文件,但异步没有。

它是从哪里来的?从车轮的数据目录看来。

所以,解决方案:我从我的 virtualenv 的根目录中删除了 kombu 目录,并且 celery 工作了。

于 2018-05-25T09:55:18.600 回答
3

我有同样的问题,但在使用 3.1.25 版本重新安装 celery 时解决了

pip uninstall celery && pip install celery==3.1.25

可能是因为 celery 4 没有官方支持 windows,https://github.com/celery/celery/issues/3551

于 2018-05-28T08:53:16.543 回答
1

我刚开始吃芹菜。我按照说明安装了 Celery v 4.2.0

当我尝试运行命令时: celery -A mysite worker -l info 出现错误:

ModuleNotFoundError: No module named 'kombu.asynchronous.timer

我删除了芹菜安装:pip uninstall celery

之后按照“chuhy”的推荐安装了 Celery 3.1.25

但是..它还有其他一些问题,所以我立即卸载了 3.1.25 ,并重新安装了 celery v4.2.0 。

在这种情况下,错误没有再次弹出。

于 2018-06-12T11:16:42.403 回答
1

我在你拥有的同一个 python 版本上测试了 celery,没关系。而且https://github.com/celery/kombu/blob/master/kombu/asynchronous/timer.py表明随机重命名事物不会对您有所帮助。也许您应该尝试pip uninstall kombu && pip --no-cache-dir install -U kombukombu. 我想你的安装肯定有问题。因此,如果kombu重新安装的东西不起作用,请尝试再次安装整个东西。

于 2018-05-24T20:22:12.733 回答
1

我遇到过类似的问题,这是因为旧版本的芹菜。卸载 celery (pip uninstall celery) 并再次安装 (pip install Celery==4.3) 并且 kaboom 它将工作。

于 2020-05-25T13:41:37.127 回答
0

我在 Windows 上工作,所以我遇到了一些问题。但我的解决方案是使用 python 3.6.8 创建新的 conda env(据我所知,芹菜可能适用于 python 3.7,但有很多问题)。然后继续安装最新版本的 celery(4.3.0) 和 Django(2.2.3),之后一切正常。

于 2019-07-12T07:12:00.323 回答
0

所以我正在运行windows 10,python3.9.x,使用aws sqs作为代理,刚刚完成了一些文件的更新:

设置.py

###
### For celery tasks!
###

from kombu.utils.url import safequote
import urllib.parse

AWS_ACCESS_KEY_ID = 'my aws_access_key_id for a user'
AWS_SECRET_ACCESS_KEY = 'my aws_secret_access_key for a user'

BROKER_URL = 'sqs://%s:%s@' % (urllib.parse.quote(AWS_ACCESS_KEY_ID, safe=''), urllib.parse.quote(AWS_SECRET_ACCESS_KEY, safe=''))
BROKER_TRANSPORT = 'sqs'

BROKER_TRANSPORT_OPTIONS = {
     'canves-celery-queue': {
          'access_key_id': safequote(AWS_ACCESS_KEY_ID),
          'secret_access_key': safequote(AWS_SECRET_ACCESS_KEY),
          'region': 'us-east-1'
     }
}

CELERY_DEFAULT_QUEUE = 'celery<-project-queue>'
CELERY_QUEUES = {
    CELERY_DEFAULT_QUEUE: {
        'exchange': CELERY_DEFAULT_QUEUE,
        'binding_key': CELERY_DEFAULT_QUEUE,
    }
}

###
### End celery tasks
###

celery_tasks.py(在本教程中称为 celery.py - 重命名是因为显然这会导致其他一些程序员出现一些错误):

from __future__ import absolute_import
import os
from celery import Celery


# Set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', '<project>.settings')

# Using a string here means the worker doesn't have to serialize
# the configuration object to child processes.
# - namespace='CELERY' means all celery-related configuration keys
#   should have a `CELERY_` prefix.

app = Celery('<project>', include=['<project>.tasks'])
app.config_from_object('django.conf:settings')

# Load task modules from all registered Django apps.
app.autodiscover_tasks()


@app.task(bind=True)
def debug_task(self):
    print("debug_task was fired!")
    print(f'Request: {self.request!r}')

if __name__ == '__main__':
    app.start()

tasks.py(这与 settings.py 在同一目录中 - 也在 celery_tasks.py 中引用)

from __future__ import absolute_import
from .celery_tasks import app
import time

@app.task(ignore_result=True)
def sleep(x, y):
    print("Sleeping for: " + str(x + y))
    time.sleep(x + y)
    print("Slept for: " + str(x + y))

当我去运行worker时(确保你和manage.py在同一个目录中),它抛出了这个错误:

from kombu.async.timer import Entry, Timer as Schedule, to_timestamp, logger

为了修复它,我按照 gogaz 的回答跑了

pip uninstall django-celery
pip uninstall celery && pip install celery

这将我推到了最新版本的 celery, 4.3 ... celery 4+ is not supported on windows as per this SO question ( Celery raises ValueError: not enough values to unpack ), 方便地有这个答案 (由 Samuel Chen 发布):

适用于 celery 4.2+,python3,windows 10

pip install gevent
celery -A <project> worker -l info -P gevent

适用于 celery 4.1+、python3、windows 10

pip install eventlet
celery -A <project> worker -l info -P eventlet

我得到的唯一另一个错误是 django 的调试器正在打开,这显然会导致内存泄漏......

问题(至少对我来说)是我不能使用 Prefork 池,这意味着我不能使用 app.control.revoke() 来终止任务。

于 2021-10-01T19:19:35.387 回答