VSCode 调试 Celery worker

VSCode debug Celery worker

经过 运行 一些令人沮丧的日子后,我需要查看 VSCode 中的 celery worker 进程的调试。这是遵循 Celery 文档中用于创建消息处理程序的建议过程,而不是来自同一应用程序的 pub/sub。

celery.py 文件:

from __future__ import absolute_import, unicode_literals
import os
import json

from celery import Celery, bootsteps
from kombu import Consumer, Exchange, Queue

dataFeedQueue = Queue('statistical_forecasting', Exchange('forecasting_event_bus', 'direct', durable=False), 'DataFeedUpdatedIntegrationEvent')

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

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

# Not required yet as handler is within this file
#app.autodiscover_tasks()


class DataFeedUpdatedHandler(bootsteps.ConsumerStep):
    def get_consumers(self, channel):
        return [Consumer(channel, queues=[dataFeedQueue],    callbacks=[self.handle_message], accept=['json'])]


def handle_message(self, body, message):
    event = json.loads(body)

    # removed for brevity, but at present echo's message content with print

    message.ack()

app.steps['consumer'].add(DataFeedUpdatedHandler)

我的缩写项目结构是:

workspace -
    vscode -
        - launch.json
    config -
        __init__.py            
        settings -
            local.py
    venv -
        celery.exe
    statistical_forecasting -
        __init__.py
        celery.py
        farms -
            __init__.py
            handlers.py    # ultimately handler code should live here...

从启用 venv 的终端,我 运行ning celery -A statistical_forecasting worker -l info 似乎确实成功设置并 运行ning 基本消息处理程序。

到目前为止,我使用 VSCode 尝试的是在 launch.json

中设置以下配置
{
    "version": "0.2.0",
    "configurations": [
    {
        "name": "Python: Celery",
        "type": "python",
        "request": "launch",
        "module": "celery",
        "console": "integratedTerminal",
        //"program": "${workspaceFolder}\env\Scripts\celery.exe",
        "args": [
            "worker",
            "-A statistical_forecasting",
            "-l info",
            ]
        },
    ]
}

不幸的是,这只会导致以下消息:

Error:
Unable to load celery application.
The module  statistical_forecasting was not found.

从逻辑上讲,我可以推断调试应该是 运行ning celery 来自工作区目录,并且它应该看到具有 __init__.py 技术制作的 statistical_forecasting 目录它是一个模块?

我尝试了其他各种想法,例如在 lauch.json 中强制设置虚拟环境等 program,但都返回了相同的基本错误消息。

statistical_forecasting 中的 'init.py' 包含标准的 Django 设置,我不相信它是必需的,因为 celery 任务是 运行 在 Django 之外,我不打算 publish/receive 来自 Django 应用程序。

为了其他尝试这样做的人的利益,这是我将芹菜作为模块进行测试的最小配置

    {
        "name": "Python: Celery",
        "module": "celery",
        "console": "integratedTerminal",
        "args": [
            "worker",
            "--app=statistical_forecasting",
            "--loglevel=INFO",
        ],
    },

关键要点是 args 的格式。原始版本使用的是 运行 形成命令行时通常会看到的缩短版本,例如在教程中。

通常你会看到 celery -A statistical_forecasting worker -l info 调试器工作你需要更完整的版本 celery --app=statistical_forecasting worker --loglevel=INFO

反映下面的评论也可以作为:

    {
        "name": "Python: Celery",
        "module": "celery",
        "console": "integratedTerminal",
        "args": [
            "worker",
            "-A",
            "statistical_forecasting",
            "-l",
            "info",
        ],
    },

出于兴趣,较长的版本如下,但这主要只是重复 VsCode 默认设置的内容:

    {
        "name": "Python: Celery",
        "type": "python",
        "request": "launch",
        "module": "celery",
        "console": "integratedTerminal",
        "cwd": "${workspaceFolder}",
        "program": "${workspaceFolder}\env\Scripts\celery.exe",
        "pythonPath": "${config:python.pythonPath}",
        "args": [
            "worker",
            "--app=statistical_forecasting",
            "--loglevel=INFO",
        ],
        "env":{
            "DJANGO_SETTINGS_MODULE": "config.settings.local",
        }
    },