[英]How to receive celery tasks from another module
I can successfully receive celery tasks from my main app - however, my system cannot receive tasks from another module. 我可以从主应用程序成功接收芹菜任务-但是,我的系统无法从其他模块接收任务。 I'm on a remote Ubuntu server using supervisor for for celery.
我在使用主管进行芹菜管理的远程Ubuntu服务器上。
draft1
is my main app and post
is another module (the one that I can't receive tasks from). draft1
是我的主要应用程序, post
是另一个模块(我无法从中接收任务的模块)。
draft1/__init__.py draft1 / __ init__.py
#This will make sure the app is always imported when
#Django starts so that shared_task will use this app.
from .celery import app as celery_app
__all__ = ['celery_app']
draft1/celery.py draft1 / celery.py
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'draft1.settings')
app = Celery("draft1", broker=CELERY_BROKER_URL, include=['post'])
app.config_from_object('django.conf:settings', namespace='CELERY')
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS) #post is in my installed apps
draft1/tasks.py draft1 / tasks.py
@periodic_task(run_every=timedelta(minutes=1))
def test_job():
from polls.models import Question
for i in Question.objects.all():
if i.question_text == "test":
i.question_text = "not_test"
i.save()
return HttpResponseRedirect('/')
@periodic_task(name='run_scheduled_jobs', run_every=timedelta(seconds=30))
def run_scheduled_jobs():
return True
post/tasks.py post / tasks.py
@periodic_task(name='test_post', run_every=timedelta(seconds=30))
def test_post():
from .models import Post
for i in Post.objects.all():
if i.entered_category == "test":
i.entered_category = "not_test"
i.save()
return HttpResponseRedirect('/')
@periodic_task(name='post_jobs', run_every=timedelta(seconds=30)) # task name found! celery will do its job
def post_jobs():
# do whatever stuff you do
return True
settings.py settings.py
CELERYBEAT_SCHEDULE = {
'run_scheduled_jobs': {
'task': 'run_scheduled_jobs', # the same goes in the task name
'schedule': timedelta(seconds=45),
},
'test_job': {
'task': 'tasks.test_job',
'schedule': timedelta(minutes=3),
},
'post_jobs': {
'task': 'post.tasks.post_jobs', #i've also tried tasks.post_jobs
'schedule': timedelta(minutes=1),
},
'test_post': {
'task': 'post.tasks.test_post',
'schedule': timedelta(seconds=45),
}
}
Here is my command to start the worker: celery -A draft1 worker -l info
这是我启动工作程序的命令:
celery -A draft1 worker -l info
and celery beat is started via: celery -A draft1 beat -l info --scheduler
. 并通过以下方式启动celery beat:
celery -A draft1 beat -l info --scheduler
。
The tasks from draft1/tasks.py
are the only tasks I receive: 我收到的唯一任务是
draft1/tasks.py
中的任务:
Why is this? 为什么是这样?
The celery -A
switch you are passing explicitly creates a worker for only the draft1
app. 您传递的
celery -A
开关仅为draft1
应用程序显式创建一个worker。 You'll want a separate worker for the post
app, as well as setting up a celery.py
for that one to, so that you can create a separate Celery instance for the post
tasks. 您需要为
post
应用程序指定一个单独的工作程序,并celery.py
设置一个celery.py
,以便您可以为post
任务创建一个单独的Celery实例。 These can obviously share the same message broker if you want (and you probably do). 显然,如果您愿意(并且您可能愿意),它们可以共享同一个消息代理。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.