使用djcelery抓取网站并填充django模型

0 投票
2 回答
1047 浏览
提问于 2025-04-16 09:10

我在用django配置celery的时候遇到了一些麻烦。我想用celery每20分钟抓取一次网站,然后更新一些django模型。

我在我的应用目录里创建了一个任务文件,里面有一个更新的类:

class Update(PeriodicTask):
    run_every=datetime.timedelta(minutes=20)

    def run(self, **kwargs):
        #update models

这个类如果我在命令行里运行的话,可以正确更新我的模型:

if __name__ == '__main__':
    Update().run()

我在settings.py里的celery配置看起来是这样的:

CELERY_RESULT_BACKEND = "database"
BROKER_HOST = 'localhost'
BROKER_PORT = 5672
BROKER_USER = 'Broker'
BROKER_PASSWORD = '*password*'
BROKER_VHOST = 'broker_vhost'

但是当我运行 manage.py celeryd -v 2 的时候,出现了连接错误:

[2010-12-29 09:28:15,150: ERROR/MainProcess] CarrotListener: Connection Error: [Errno 111] Connection refused. Trying again in 10 seconds...

我漏掉了什么呢?

更新:

我发现了 django-kombu,看起来不错,因为它可以使用我现有的数据库。我已经安装了django-kombu和kombu,但现在在运行 manage.py celeryd -v 2 时出现了以下错误。

Traceback (most recent call last):
  File "manage.py", line 11, in <module>
    execute_manager(settings)
  File "<webapp_path>/lib/python2.6/django/core/management/__init__.py", line 438, in execute_manager
    utility.execute()
  File "<webapp_path>/lib/python2.6/django/core/management/__init__.py", line 379, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "<webapp_path>/lib/python2.6/django/core/management/base.py", line 191, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "<webapp_path>/lib/python2.6/django/core/management/base.py", line 220, in execute
    output = self.handle(*args, **options)
  File "<webapp_path>/lib/python2.6/django_celery-2.1.4-py2.6.egg/djcelery/management/commands/celeryd.py", line 20, in handle
    worker.run(*args, **options)
  File "<webapp_path>/lib/python2.6/celery-2.1.4-py2.6.egg/celery/bin/celeryd.py", line 83, in run
    from celery.apps.worker import Worker
  File "<webapp_path>/lib/python2.6/celery-2.1.4-py2.6.egg/celery/apps/worker.py", line 15, in <module>
    from celery.task import discard_all
  File "<webapp_path>/lib/python2.6/celery-2.1.4-py2.6.egg/celery/task/__init__.py", line 7, in <module>
    from celery.execute import apply_async
  File "<webapp_path>/lib/python2.6/celery-2.1.4-py2.6.egg/celery/execute/__init__.py", line 7, in <module>
    from celery.result import AsyncResult, EagerResult
  File "<webapp_path>/lib/python2.6/celery-2.1.4-py2.6.egg/celery/result.py", line 9, in <module>
    from celery.backends import default_backend
  File "<webapp_path>/lib/python2.6/celery-2.1.4-py2.6.egg/celery/backends/__init__.py", line 51, in <module>
    default_backend = DefaultBackend()
TypeError: __init__() takes exactly 2 arguments (1 given)

2 个回答

0

我也遇到过同样的问题,问题出在我导入的路径不对。

你可能是这样导入的:

from celery import task

其实你应该这样导入:

from celery.task import task

1

看起来你没有安装或运行任何代理软件(比如RabbitMQ)。

撰写回答