【问题标题】:How to run a task with celery in django and save result in django database?如何在 django 中使用 celery 运行任务并将结果保存在 django 数据库中?
【发布时间】:2016-03-31 01:59:45
【问题描述】:

我已经制作了一个刮板来从网页中抓取一些链接,并希望每 1 小时运行一次此刮板,它驻留在 django 应用程序中,但是 django 不可能每 1 小时运行一次刮板,因为 django 视图取决于请求响应对象。为了解决这个问题,我决定使用一个名为 celery 的 python 库,并根据文档编写了 celery.py 和 tasks.py 文件

django的项目结构是这样的

newsportal
 - newsportal
    -settings.py 
    -celery.py
    __init__.py
 - news
    -tasks.py
    -views.py 
    -models.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', 'newsportal.settings')

from django.conf import settings  # noqa

app = Celery('newsportal')

# Using a string here means the worker will not have to
# pickle the object when using Windows.
app.config_from_object('django.conf:settings')
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)


@app.task(bind=True)
def debug_task(self):
    print('Request: {0!r}'.format(self.request))

__init__.py文件有以下几行代码

from __future__ import absolute_import

# This will make sure the app is always imported when
# Django starts so that shared_task will use this app.
from .celery import app as celery_app  # noqa

tasks.py 有以下代码行

from __future__ import absolute_import
from celery import shared_task
from crawler import crawler
from .models import News

@shared_task
def news():
    '''
    scrape all links
    '''
    news = [] #store dict object
    allnews.append(crawler()) 
    for news_dict in allnews:
        for news, url  in news_dict.items():
            #Save all the scrape news in database
            News.objects.create(title=news, url=url, source=source)

我想要做的是每 1 小时运行一次上述 news() 函数并将结果保存到数据库中。

我想将任务的结果保存到 django 数据库中,我该如何实现呢。

根据 celery 文档,为了保存工作人员给出的结果,我们需要安装 django-celery==3.1.17,因为我已经安装了,然后进行迁移。

根据 celery 文档,对于 celery 中的数据库后端,我们应该把

app.conf.update(
    CELERY_RESULT_BACKEND='djcelery.backends.database:DatabaseBackend',
)



line of code on settings.py file, on putting this of code in `settings.py` file I got the error of

settings.py", line 141, in <module>
    app.conf.update(
NameError: name 'app' is not defined

因为我已经导入并将以下代码行放入settings.py 文件中,如下所示

from __future__ import absolute_import
BROKER_URL = 'redis://localhost'

我最想做的是,

  1. 每 1 小时运行一次上述爬虫并保存结果 数据库中的爬虫称为新闻 我如何使用 celery 完成此任务,或者我错过了什么?

是否有其他替代方法来完成此任务

【问题讨论】:

  • 当然 celery 是长期任务的最佳工具。但是您提供了太多信息,并没有告诉我们您具体使用芹菜有什么问题。你能让芹菜跑起来吗?有什么错误吗?
  • 我想每 1 小时运行一次爬虫,并将爬虫结果保存在 django db 中,这是我想要的,但我迷失了这个 celery 配置。我已经编写了运行爬虫的 celery.py 文件 tasks.py 文件,但是我不知道使用 celery 运行该爬虫
  • 调用“爬虫”不同于任何其他异步任务;这是一个绝对标准的 Celery 配置。您应该指出 Celery 文档中据称告诉您将 app.conf.update 放在那里的部分。

标签: python django django-views django-celery celery-task


【解决方案1】:

如果您想在那里添加该配置,我相信您会在 celery.py 中使用 app.conf.update(...)

您在celery.py 中的app.config_from_object('django.conf:settings') 调用表明您正在从settings.py 文件加载配置设置。

所以您应该可以将CELERY_RESULT_BACKEND='djcelery.backends.database:DatabaseBackend' 放在settings.py 文件的末尾。

这应该可以防止您收到该错误。

【讨论】:

    猜你喜欢
    • 2016-05-07
    • 2014-03-16
    • 2021-02-21
    • 2022-11-25
    • 1970-01-01
    • 2019-08-21
    • 1970-01-01
    • 2020-06-30
    • 2014-12-15
    相关资源
    最近更新 更多