如何从另一个模块接收芹菜任务

时间:2018-04-29 02:33:17

标签: python django celery supervisord

我可以从主应用程序成功接收芹菜任务 - 但是,我的系统无法从其他模块接收任务。我在一个远程Ubuntu服务器上使用supervisor进行芹菜。

draft1是我的主要应用,post是另一个模块(我无法接收任务的模块)。

draft1 / __初始化__。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

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

@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

发表/ 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

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),
    }
}

这是启动工作人员的命令:celery -A draft1 worker -l info

和芹菜节拍通过以下方式开始:celery -A draft1 beat -l info --scheduler

draft1/tasks.py中的任务是我收到的唯一任务:

enter image description here

为什么会这样?

1 个答案:

答案 0 :(得分:0)

您传递的celery -A开关明确地为draft1应用创建了一个工作线程。您需要为post应用设置单独的工作人员,并为该设备设置celery.py,以便您可以为post创建单独的Celery实例任务。如果你愿意,这些显然可以共享相同的消息代理(并且你可能会这样做)。