【问题标题】:Django-Celery Scheduling Daily Tasks on Windows ServerDjango-Celery 在 Windows Server 上安排日常任务
【发布时间】:2019-07-10 02:02:33
【问题描述】:

我必须安排一个任务在每周一到周六运行。我下载了 django-celery 来完成任务。我按照Celery site 上的教程进行操作,并使用并查看了一些堆栈溢出帖子,例如:here & here

我对上述帖子和这两个很棒的教程进行了混合搭配:RhatoreFreitas

这是我的文件夹结构:

Application
    | apps
         | app1
              | __init__.py
              | tasks.py
              | urls.py
              | views.py    
    | Application
         | __init__.py
         | celery.py
         | settings.py
         | urls.py
         | wsgi.py

Settings.py

INSTALLED_APPS = [
    'django_celery_beat',
    'apps.app1',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True    
USE_TZ = True

CELERY_BROKER_URL = 'amqp://localhost'
CELERY_RESULT_BACKEND = 'amqp://localhost'
CELERY_ACCEPT_CONTENT = ['application/json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
CELERY_TIMEZONE = 'UTC'

__init__.pyinit下的Application

from __future__ import absolute_import, unicode_literals
from .celery import app as celery_app

from .celery import app as celery_app

    __all__ = ('celery_app',)

celery.py

from __future__ import absolute_import, unicode_literals
import os
from celery import Celery

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'Application.settings')

app = Celery('Application')

app.config_from_object('django.conf:settings', namespace='CELERY')
app.autodiscover_tasks()

@app.task(bind=True)
def debug_task(self):
    print('Request: {0!r}'.format(self.request))

tasks.py - 为简单起见,我将任务简化为简单的打印命令

from celery import shared_task
from celery.utils.log import get_task_logger
from celery.schedules import crontab
from celery.task import periodic_task
from celeryapp.emails import send_feedback_email
from django.http import HttpResponse
from .models import *

logger=get_task_logger(__name__)

@periodic_task(run_every=crontab(hour=21, minute=32, day_of_week=0-5))
def simple_print():
    print("Hello World!")

我跑了:

celery -A Application -1 info

然后python manage.py runserver 21:32 UTC 过去了,它没有打印。我希望根据 tasks.py 中指定的任务在 21:32 UTC 在终端中看到“Hello World”。它没有打印。

我也跑了:

celery -A Application worker -l info
celery -A Application beat -l info
python manage.py runserver

在不同的终端。它没有执行任务。

非常感谢您的指导。

【问题讨论】:

  • 您能否更详细地解释您的问题。或者也许“它没有打印” - 是对问题的解释?你期待什么?
  • 亚历克斯感谢您的提问。我编辑了我的问题。基本上,我要做的是使用 cron 将“Hello World”打印到终端。
  • 如果您将print 替换为文件-writelogging.info 或者数据库插入 - 您会得到“Hello World!”吗?
  • 我试过 f = open("myfile.txt", "x") 并没有创建文件。

标签: django rabbitmq celery


【解决方案1】:

你需要让worker和beat都运行,这样celery就会知道它是否必须在某个时间运行一个周期性任务。

这样的事情应该可以帮助您在本地环境中。

celery -A Application worker -l info -B

或者,您可以将两个工作人员作为单独的服务启动

celery -A Application worker -l info

celery -A Application beat -l info

【讨论】:

  • 我尝试使用 -B 并表示 -B 在 Windows 上不起作用。我如何单独运行它们?不同的终端?我可以在任何目录中还是需要在 app 目录中?我什么时候在启动工人之前或之后运行 manage.py runserver?它必须在另一个终端窗口中吗?
  • 3 个终端/tmux 会话:一个用于 celery worker,一个用于 beat,然后一个用于 runserver。
  • 我打开了 3 个窗口。导航到每个应用程序文件夹。运行worker、beat和python manage.py runserver,它没有打印出来。
  • 可能是时区问题或缺少配置。没有堆栈跟踪或除了简短描述之外的其他东西的调试非常乏味。
【解决方案2】:

代码是正确的。问题是由于操作系统。我使用的是 Windows,Celery 4.0 不支持 Windows。这个问题非常有用:How to Run Celery on Windows。我安装了 gevent,它现在可以工作了。

pip install gevent
celery -A Application beat -l info
celery -A Application worker -l info -P gevent

如果您遇到另一个类似“启动器中的致命错误...”的错误,请尝试:

python -m celery -A Application beat -l info
python -m celery -A Application worker -l info -P gevent

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-05-24
    • 2013-09-30
    • 2010-10-22
    • 2012-02-19
    • 2020-05-08
    • 2019-08-20
    • 2012-01-16
    • 1970-01-01
    相关资源
    最近更新 更多