简体   繁体   English

芹菜和路由

[英]Celery and routing

I need to run some tasks on the specific celeryd instance. 我需要在特定的celeryd实例上运行一些任务。 So I configured queues: 所以我配置了队列:

celeryconfig.py: celeryconfig.py:

CELERY_QUEUES = {
    'celery': {
        'exchange': 'celery',
        'binding_key': 'celery',
    },
    'import': {
        'exchange': 'import',
        'binding_key': 'import.products',
    },
}

CELERY_ROUTES = {
    'celery_tasks.import_tasks.test': {
        'queue': 'import',
        'routing_key': 'import.products',
    },
}

import_tasks.py: import_tasks.py:

@task
def test():
    print 'test'

@task(exchange='import', routing_key='import.products')
def test2
    print 'test2'

then I start celeryd: 然后我开始芹菜:

celeryd -c 2 -l INFO -Q import

And try to execute that tasks. 并尝试执行该任务。 'test' executes but 'test2' do not. 'test'执行但'test2'没有执行。 But I don't want to specify every importing task in the CELERY_ROUTES. 但我不想在CELERY_ROUTES中指定每个导入任务。 How can I specify which queue should execute task in the task definition? 如何在任务定义中指定哪个队列应该执行任务?

请参阅Roman的解决方案 - http://www.imankulov.name/posts/celery-for-internal-api.html - 按名称访问任务,但也能够指定队列以及如何导入任务模块。

Oh, forgot to say that I've used send_task function to execute tasks. 哦,忘了说我已经使用了send_task函数来执行任务。 And this function doesn't import tasks. 此功能不会导入任务。 It just sends the name of the task to the queue. 它只是将任务的名称发送到队列。

So instead of this: 所以不是这样的:

from celery.execute import send_task

result = send_task(args.task, task_args, task_kwargs)

I wrote: 我写:

from celery import current_app as celery_app, registry as celery_registry

celery_imports = celery_app.conf.get('CELERY_IMPORTS')
if celery_imports:
    for module in celery_imports:
        __import__(module)

task = celery_registry.tasks.get(args.task)
if task:
    result = task.apply_async(task_args, task_kwargs)

I found solution that almost satisfied me: 我找到了几乎让我满意的解决方案:

class CustomRouter(object):
    def route_for_task(self, task, args=None, kwargs=None):
        if task.startswith('celery_tasks.import_tasks'):
            return {'exchange': 'import',
                    'routing_key': 'import.products'}

CELERY_ROUTES = (
    CustomRouter(),
)

Problem is that now I can't use names for tasks. 问题是现在我不能使用名称来完成任务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM