VSCode 调试 Celery Worker

Ros*_*day 5 python django celery visual-studio-code vscode-debugger

经过几天令人沮丧的运行之后,我需要考虑在 VSCode 中调试 celery 工作进程。这是遵循 Celery 文档中创建消息处理程序的建议流程,而不是从同一应用程序发布/订阅。

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)
Run Code Online (Sandbox Code Playgroud)

我的简短项目结构是:

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...
Run Code Online (Sandbox Code Playgroud)

我正在从带有 venv enable 的终端运行,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",
            ]
        },
    ]
}
Run Code Online (Sandbox Code Playgroud)

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

Error:
Unable to load celery application.
The module  statistical_forecasting was not found.
Run Code Online (Sandbox Code Playgroud)

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

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

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

Ros*_*day 10

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

    {
        "name": "Python: Celery",
        "module": "celery",
        "console": "integratedTerminal",
        "args": [
            "worker",
            "--app=statistical_forecasting",
            "--loglevel=INFO",
        ],
    },
Run Code Online (Sandbox Code Playgroud)

关键要点是参数的格式如何。原始版本使用的是您通常在从命令行运行时看到的缩短版本,例如在教程中。

通常,您会发现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",
        ],
    },
Run Code Online (Sandbox Code Playgroud)

出于兴趣,较长的版本如下,但这主要只是重复 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",
        }
    },
Run Code Online (Sandbox Code Playgroud)