我在让Celery/Celery Beat计划除我的Celery.py文件中的任务之外的任何任务时遇到问题。我可以看到任务注册为"芹菜检查注册",但任务不按计划运行。我读了所有的文件,我是一个碰壁的人。使用Ubuntu WSL在Windows上运行Redis。
测试-每10秒运行一次,并显示在我的外壳中PayClosedLines-已注册,但当我管理我的芹菜工人时,它不会出现在我的外壳中。
/proj/proj/celery.py
from __future__ import absolute_import
import os
from celery import Celery
from django.apps import apps
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings')
app = Celery('mysite')
app.config_from_object('django.conf:settings')
app.autodiscover_tasks(lambda: [n.name for n in apps.get_app_configs()])
@app.task(bind=True)
def debug_task(self):
print('Request: [0!r}'.format(self.request))
@app.on_after_configure.connect
def setup_periodic_tasks(sender, **kwargs):
sender.add_periodic_task(10.0, test.s('test'), name='add every 10')
sender.add_periodic_task(10.0, )
sender.add_periodic_task(30.0, test.s(), expires=10)
@app.task
def test(arg):
print(arg)
'''
proj/proj/settings.py
BROKER_URL = 'redis://localhost:6379'
CELERY_RESULT_BACKEND = 'redis://localhost:6379'
CELERY_ACCEPT_CONTENT = ['application/json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
CELERY_TIMEZONE = 'UTC'
from datetime import timedelta
CELERY_BEAT_SCHEDULE = {
'Payout bets every 10 seconds': {
'task': 'UFCBetting.tasks.PayClosedLines',
'schedule': timedelta(seconds=10.0),
},
}
CELERY_IMPORTS = ('UFCBetting.tasks',)
proj/app/task.py
from __future__ import absolute_import, unicode_literals
from .MyBookieTools import get_odds
from .models import BettingLines, Bets, CustomUser
from celery import task, shared_task
@task(name='UpdateLinesTable')
def UpdateLinesTable():
odds = get_odds()
for odd in odds:
bl = BettingLines()
bl.fighter = odd[0]
bl.line = odd[1]
bl.save()
@shared_task
def PayClosedLines():
unpaid_lines = BettingLines.objects.filter(result="W").exclude(payment_status=True)
print(unpaid_lines)
for line in unpaid_lines:
print(line)
if line.result == "W":
unpaid_bets = Bets.objects.filter(line_id=line.id)
print(unpaid_bets)
for bet in unpaid_bets:
user = CustomUser.objects.get(id=bet.placed_by_id)
user.balance = user.balance + line.payout(amount=bet.bet_amount)
user.save()
line.payment_status = 1
line.save()
print("Closed lines paid.")
您的设置没有CELERY_BEAT_SCHEDULER
常量。
如果将其添加到设置中,则不需要使用--scheduler
选项
CELERY_BEAT_SCHEDULER = `django_celery_beat.schedulers:DatabaseScheduler`
我安装了django芹菜节拍,所以我需要像这样启动我的节拍工作者。
celery -A mysite beat -l info --scheduler django_celery_beat.schedulers:DatabaseScheduler
而不是通常的
celery -A mysite beat