VSCode调试Celery worker

时间:2019-08-29 09:22:58

标签: python django visual-studio-code celery vscode-debugger

运行了几天令人沮丧的日子之后,我需要研究如何在VSCode中调试芹菜工作者进程。这是遵循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目录使其成为一个模块?

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

statistics_forecasting中的' init .py'包含标准的Django设置,我不认为这是必需的,因为celery任务是在Django外部运行的,我不打算发布/接收从Django应用程序中获取。

1 个答案:

答案 0 :(得分:0)

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

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

主要说明ID如何格式化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",
        }
    },