首页 > 解决方案 > VSCode调试芹菜工人

问题描述

在运行了一些令人沮丧的日子之后,我需要查看在 VSCode 中调试 celery 工作进程。这是遵循 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 的终端开始,我正在运行celery -A statistical_forecasting worker -l info它似乎确实成功地设置和运行了基本消息处理程序。

到目前为止,我尝试使用 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.

从逻辑上讲,我可以推断应该celery从工作区目录运行调试,并且它应该看到statistical_forecasting具有__init__.py技术使其成为模块的目录?

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

Statistics_forecasting 中的“ init .py”包含标准的 Django 设置,我不相信它是必需的,因为 celery 任务是在 Django 之外运行的,我不打算从 Django 应用程序发布/接收。

标签: pythondjangovisual-studio-codeceleryvscode-debugger

解决方案


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

    {
        "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",
        }
    },

推荐阅读