如何从 Django 启动远程芹菜工人

How to start remote celery workers from django

我正在尝试将 django 与 celery 结合使用。

因此我遇到了 autodiscover_tasks() 但我不确定如何使用它们。芹菜工人获得其他应用程序添加的任务(在本例中为节点后端)。

到目前为止,我用这个来启动 worker:

celery worker -Q extraction --hostname=extraction_worker

效果很好。

现在我不确定 django-celery 集成的总体思路是什么。 worker 应该仍然从外部启动(例如使用上面的命令),还是应该从 django 应用程序管理和启动它们?

我的 celery.py 看起来像:

# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'main.settings')

app = Celery('app')

app.config_from_object('django.conf:settings', namespace='CELERY')

# Load task modules from all registered Django app configs.
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)

然后我有 2 个应用程序包含一个 tasks.py 文件:

@shared_task
def extraction(total):
    return 'Task executed'

我现在如何注册 django 来为这些任务注册工作人员?

您只需以 documented 的身份启动工作进程,您不需要注册任何其他内容

In a production environment you’ll want to run the worker in the background as a daemon - see Daemonization - but for testing and development it is useful to be able to start a worker instance by using the celery worker manage command, much as you’d use Django’s manage.py runserver:

celery -A proj worker -l info

For a complete listing of the command-line options available, use the help command:

celery help

celery worker collects/registers task 当它运行并消耗它发现的任务时