问题是我得到了以下日志:
celery_1 | [2021-03-15 19:00:00,124: INFO/MainProcess] Scheduler: Sending due task read_dof (api.tasks.read_dof)
celery_1 | [2021-03-15 19:00:00,140: INFO/MainProcess] Scheduler: Sending due task read_bdm (api.tasks.read_bdm)
celery_1 | [2021-03-15 19:00:00,141: INFO/MainProcess] Scheduler: Sending due task read_fixer (api.tasks.read_fixer)
我有以下芹菜的配置。Exchange是我的django项目的名称,这里是“芹菜.py”,api是我的django应用程序的名称,这里是我的“tasks.py”:
from __future__ import absolute_import, unicode_literals
import os
from celery import Celery
from celery.schedules import crontab
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "exchange.settings")
app = Celery("exchange")
app.config_from_object("django.conf:settings", namespace="CELERY")
app.autodiscover_tasks()
app.conf.beat_schedule = {
'read_bdm': {
'task': 'api.tasks.read_bdm',
'schedule': crontab(hour=19,minute=0),
},
'read_dof': {
'task': 'api.tasks.read_dof',
'schedule': crontab(hour=19,minute=0),
},
'read_fixer': {
'task': 'api.tasks.read_fixer',
'schedule': crontab(hour=19,minute=0),
},
}
以下是我的任务.py:
from celery import shared_task
from .models import BdmExch, DofExch, FixerExch
from .helpers.bdmcrawler import parse_bdm
from .helpers.dofcrawler import parse_dof
from .helpers.fixercrawler import parse_fixer
@shared_task(name='read_bdm')
def read_bdm():
attempts=0
while attempts <3:
try:
result = parse_bdm()
print(result)
BdmExch.objects.create(time=result["date"],exch=result["exc"])
return
except:
attempts += 1
print("Parsing error on read_bdm")
print("--------------- Parsing error on read_bdm -----------")
return
@shared_task(name='read_dof')
def read_dof():
attempts=0
while attempts < 3:
try:
result = parse_dof()
DofExch.objects.create(time=result["date"],exch=result["exc"])
return
except:
attempts += 1
print("Parsing error on read_dof")
print("--------------- Parsing error on read_dof -----------")
return
@shared_task(name='read_fixer')
def read_fixer():
attempts=0
while attempts < 3:
try:
result = parse_bdm()
FixerExch.objects.create(time=result["date"],exch=result["exc"])
return
except:
attempts += 1
print("Parsing error on read_fixer")
print("--------------- Parsing error on read_fixer -----------")
return
正如我在api django应用程序中所说的,parse_bdm、parse_dof和parse_fixer函数是请求和美化组或简单字典的简单实现,以便从不同的源读取数据。当我简单地将任务函数当作简单函数来运行时,不会出现任何问题,因此这让我相信芹菜.py中存在一个我似乎无法确定的问题
非常感谢您的帮助
多谢各位
您是否尝试过为Django应用程序添加init文件
exchange/__init__.py
据我所知,文档有时会产生误导:
相关问题 更多 >
编程相关推荐