Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django和芹菜-ModuleNotFoundError:没有名为';芹菜.任务';_Python_Django_Celery_Periodic Task - Fatal编程技术网

Python Django和芹菜-ModuleNotFoundError:没有名为';芹菜.任务';

Python Django和芹菜-ModuleNotFoundError:没有名为';芹菜.任务';,python,django,celery,periodic-task,Python,Django,Celery,Periodic Task,我想在Django中使用芹菜开始一项定期任务。以下是我的项目的相关部分: # celery.py from celery import Celery # set the default Django settings module for the 'celery' program. os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'bookProjectSetting.settings') app = Celery('bookProjectS

我想在
Django
中使用
芹菜开始一项定期任务。以下是我的项目的相关部分:

# celery.py

from celery import Celery

# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'bookProjectSetting.settings')

app = Celery('bookProjectSetting')

# 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.config_from_object('django.conf:settings', namespace='CELERY')

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


@app.task(bind=True)
def debug_task(self):
    print(f'Request: {self.request!r}')
\uuuu init\uuuu.py
如下所示:

# 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

__all__ = ('celery_app',)
from books.models import Book
from celery.schedules import crontab
from celery.decorators import periodic_task

@periodic_task(
    run_every=(crontab(minute='*/10')), 
    name="update_life_time_of_books", 
    ignore_result=True)
def update_life_time_of_books():
   # do sth. 
My
books
app文件夹中的My
tasks.py
文件如下所示:

# 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

__all__ = ('celery_app',)
from books.models import Book
from celery.schedules import crontab
from celery.decorators import periodic_task

@periodic_task(
    run_every=(crontab(minute='*/10')), 
    name="update_life_time_of_books", 
    ignore_result=True)
def update_life_time_of_books():
   # do sth. 
我还设置了
redis
作为代理,并安装了
django芹菜beat
之类的东西。但当我通过命令运行worker时:

celery -A bookProjectSetting beat -l INFO --scheduler django_celery_beat.schedulers:DatabaseScheduler
然后我得到以下错误:

File "/home/ac3l1k/Desktop/PROJECT/simpleDjangoProjects/bookProject/env/lib/python3.8/site-packages/celery/local.py", line 403, in _compat_periodic_task_decorator
    from celery.task import periodic_task
ModuleNotFoundError: No module named 'celery.task'
从这个错误中,我认为装饰师出了问题,但我说不出原因。希望有人能帮忙

Thnks预先通知:=)

注意:以下是我的
requirements.txt
文件的内容,供版本参考

$pip冻结

amqp==5.0.1
asgiref==3.2.10
billiard==3.6.3.0
celery==5.0.1
click==7.1.2
click-didyoumean==0.0.3
click-repl==0.1.6
Django==3.1.2
django-celery-beat==2.1.0
django-timezone-field==4.0
djangorestframework==3.12.1
kombu==5.0.2
prompt-toolkit==3.0.8
python-crontab==2.5.1
python-dateutil==2.8.1
pytz==2020.1
redis==3.5.3
six==1.15.0
sqlparse==0.4.1
vine==5.0.0
wcwidth==0.2.5

正如我从本期文章中了解到的,他们不赞成该功能:

现在,您似乎需要使用它来制定计划任务:

版本5.x中不再包含“任务” 您可以使用版本4.x

  • pip3卸载芹菜
  • pip3安装芹菜==4.4.2
编辑空间