【问题标题】:Why am I getting name 'shared_task' is not defined error with Celery为什么我得到名称 'shared_task' is not defined 芹菜错误
【发布时间】:2020-12-16 01:47:42
【问题描述】:

我正在通过关注这个tutorial来学习芹菜。

我像他一样创建了任务并尝试运行代码。但我收到以下错误

NameError: name 'shared_task' is not defined

这是我的代码

import requests
from bs4 import BeautifulSoup
import json
from datetime import datetime
import lxml

@shared_task
def scrape():
    try:
        ...

此代码对他有用,但我收到上述错误。不知道我做错了什么。请帮帮我

提前致谢

【问题讨论】:

  • 需要导入from celery import shared_task
  • 也许还让文章作者知道它缺少必需的导入。

标签: python django celery celery-task


【解决方案1】:

您需要将celery shared_task decorator 导入为

from celery import shared_task


@shared_task
def scrape():
    ...
    # your code

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-09-06
    • 1970-01-01
    • 1970-01-01
    • 2021-09-08
    • 1970-01-01
    • 2020-04-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多