Django-Celery 在 Windows 服务器上安排日常任务
Django-Celery Scheduling Daily Tasks on Windows Server
我必须每周一到周六安排 运行 的任务。我下载了 django-celery 来完成任务。我遵循了 Celery site and I used and looked at a few stack overflow posts, for example: here &
上的教程
我混合搭配了上述帖子和这两个很棒的教程:Rhatore and Freitas
这是我的文件夹结构:
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__.py在init下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
在不同的终端。它没有执行任务。
非常感谢指导。
你需要同时拥有 worker 和节拍 运行ning,这样 celery 就会知道它是否必须在特定时间 运行 执行周期性任务。
类似的内容应该对您的本地环境有所帮助。
celery -A Application worker -l info -B
或者,您可以将两个 worker 作为单独的服务启动
celery -A Application worker -l info
celery -A Application beat -l info
代码正确。问题是由于 OS。我正在使用 Windows 而 Celery 4.0 不支持 Windows。这个问题非常有用:。我安装了 gevent,现在可以使用了。
pip install gevent
celery -A Application beat -l info
celery -A Application worker -l info -P gevent
如果您遇到与 "Fatal error in launcher..." 类似的错误,请尝试:
python -m celery -A Application beat -l info
python -m celery -A Application worker -l info -P gevent
我必须每周一到周六安排 运行 的任务。我下载了 django-celery 来完成任务。我遵循了 Celery site and I used and looked at a few stack overflow posts, for example: here &
我混合搭配了上述帖子和这两个很棒的教程:Rhatore and Freitas
这是我的文件夹结构:
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__.py在init下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
在不同的终端。它没有执行任务。
非常感谢指导。
你需要同时拥有 worker 和节拍 运行ning,这样 celery 就会知道它是否必须在特定时间 运行 执行周期性任务。
类似的内容应该对您的本地环境有所帮助。
celery -A Application worker -l info -B
或者,您可以将两个 worker 作为单独的服务启动
celery -A Application worker -l info
celery -A Application beat -l info
代码正确。问题是由于 OS。我正在使用 Windows 而 Celery 4.0 不支持 Windows。这个问题非常有用:
pip install gevent
celery -A Application beat -l info
celery -A Application worker -l info -P gevent
如果您遇到与 "Fatal error in launcher..." 类似的错误,请尝试:
python -m celery -A Application beat -l info
python -m celery -A Application worker -l info -P gevent