VSCode调试芹菜

2024-05-16 22:34:56 发布

您现在位置:Python中文网/ 问答频道 /正文

经过几天令人沮丧的运行之后,我需要看看如何在VSCode中调试一个celery-worker进程。这是按照Celery文档中建议的流程来创建消息处理程序,而不是从同一个应用程序创建pub/sub。在

在芹菜.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)

我的项目结构是:

^{pr2}$

从带有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",
            ]
        },
    ]
}

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

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

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

我尝试过其他各种方法,比如强制program设置虚拟环境等等,但是都返回了相同的基本错误消息。在

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


Tags: djangofrompyimportjsonapp消息message
1条回答
网友
1楼 · 发布于 2024-05-16 22:34:56

为了让其他人受益,这里是我测试芹菜作为一个模块的最小配置

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

密钥外卖标识如何格式化参数。原始版本使用的是从命令行运行时通常会看到的缩短版本,例如在教程中。在

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

它还反映了以下评论:

^{pr2}$

出于兴趣,较长的版本如下所示,但这主要是重复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",
        }
    },

相关问题 更多 >