python - 如何在 django 中使用 celery 运行任务并将结果保存在 django 数据库中?

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

我制作了一个抓取器来从网页上抓取一些链接,并希望每 1 小时运行一次这个抓取器,它驻留在 django 应用程序中,但是 django 不可能每 1 小时运行一次抓取器,因为 django View 取决于请求响应对象。为了解决这个问题,我决定使用一个名为 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,因为我已经安装了,并进行迁移。 enter image description here

根据celery docs,对于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.py 中添加该配置,我相信您会在 app.conf.update(...) 中使用该配置。

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

因此,您应该能够将 CELERY_RESULT_BACKEND='djcelery.backends.database:DatabaseBackend' 放在 settings.py 文件的末尾。

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

关于python - 如何在 django 中使用 celery 运行任务并将结果保存在 django 数据库中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34455917/

相关文章:

Python 使用请求更改 Accept-Language

python - 开始用三角形平铺屏幕

python - django celery 收到类型为 'print_test' 的未注册任务

python - 如何在基于类的 View Django 中应用装饰器做调度方法

Django __init__() 需要 1 个位置参数,但给出了 2 个

Django : login() within Sign In form clean() or in the view ?

python - 如何使用 openpyxl 普通工作簿查找列中的最后一行?

python - 如何在GAE中的NDB中执行投影查询

python - 类型错误 : tuple indices must be integers, 不是 str Python/django

python - 关闭 django mysql 连接