![](/img/trans.png)
[英]python celery - how to add CELERYBEAT_SCHEDULE task at runtime to a worker?
[英]How to configure CELERYBEAT_SCHEDULE in Django settings?
我可以让它作为一个独立的应用程序运行,但我无法让它在 Django 中工作。
from celery import Celery
from celery.schedules import crontab
app = Celery('tasks')
app.conf.update(
CELERY_TASK_SERIALIZER='json',
CELERY_RESULT_SERIALIZER='json',
CELERY_ACCEPT_CONTENT=['json'],
CELERY_TIMEZONE='US/Central',
CELERY_ENABLE_UTC=True,
CELERYBEAT_SCHEDULE = {
'test': {
'task': 'tasks.test',
'schedule': crontab(),
},
}
)
@app.task
def test():
with open('test.txt', 'a') as f:
f.write('Hello, World!\n')`
它提供给 Rabbitmq 服务器并每分钟写入一次文件。 它就像一个魅力,但是当我尝试让它在 Django 中工作时,我收到了这个错误:
您是否记得导入包含此任务的模块? 或者,也许您正在使用相对导入? 请参阅____了解更多信息。
消息正文的完整内容是:{'retries': 0, 'eta': None, 'kwargs': {}, 'taskset': None, 'timelimit': [None, None], 'callbacks': None , 'task': 'proj.test', 'args': [], 'expires': None, 'id': '501ca998-b5eb-4ba4-98a8-afabda9e88dd', 'utc': True, 'errbacks':无,'和弦':无}(246b)回溯(最近一次通话):文件“/home/user/CeleryDjango/venv/lib/python3.5/site-packages/celery/worker/consumer.py”,行456,在 on_task_received 策略[名称](消息,正文,KeyError:'proj.test' [2016-06-16 01:16:00,051: INFO/Beat] 调度程序:发送到期任务测试(proj.test)[2016- 06-16 01:16:00,055: ERROR/MainProcess] 收到类型为“proj.test”的未注册任务。
# CELERY STUFF
CELERY_ACCEPT_CONTENT = ['application/json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
CELERY_TIMEZONE = 'US/Central'
CELERYBEAT_SCHEDULE = {
'test': {
'task': 'proj.test',
'schedule': crontab(),
}
}
芹菜.py
from __future__ import absolute_import
import os
from celery import Celery
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'proj.settings')
from django.conf import settings # noqa
app = Celery('proj')
app.config_from_object('django.conf:settings')
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)
@app.task(bind=True)
def debug_task(self):
print('Request: {0!r}'.format(self.request))
任务.py
from __future__ import absolute_import
from celery import shared_task
@shared_task
def test():
with open('test.txt', 'w') as f:
print('Hello, World', file=f)
初始化文件
from __future__ import absolute_import
from .celery import app as celery_app
对此的任何想法都非常感谢。 谢谢。
你为什么不试试下面的方法,让我知道它是否适合你。 它对我有用。
在settings.py中
CELERYBEAT_SCHEDULE = {
'my_scheduled_job': {
'task': 'run_scheduled_jobs', # the same goes in the task name
'schedule': crontab(),
},
}
而在tasks.py ..
from celery.task import task # notice the import of task and not shared task.
@task(name='run_scheduled_jobs') # task name found! celery will do its job
def run_scheduled_jobs():
# do whatever stuff you do
return True
但是,如果您正在寻找 shared_task 那么..
@shared_task(name='my_shared_task') # name helps celery identify the functions it has to run
def my_shared_task():
# do what you want here..
return True
我将共享任务用于异步作业..所以我需要从如下函数中调用它..
在您的项目应用程序中的 views.py / 或任何地方.py
def some_function():
my_shared_task.apply_async(countdown= in_seconds)
return True
以防万一,如果您忘记了,请记住包含您尝试在其中运行任务的应用程序..
INSTALLED_APPS = [...
'my_app'...
] # include app
我确定这种方法工作正常..谢谢
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.