gpt4 book ai didi

python - VSCode 调试 Celery Worker

转载 作者:太空宇宙 更新时间:2023-11-03 20:27:46 45 4
gpt4 key购买 nike

经过几天令人沮丧的运行之后,我需要考虑在 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)

我的简短项目结构是:

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 -Astatistic_forecastingworker -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,并且它应该看到带有__init__.py的statistical_forecasting目录。 技术使其成为一个模块?

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

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

最佳答案

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

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

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

通常您会看到celery -Astatistical_forecastingworker -l info,调试器才能工作,您需要更完整的版本celery --app=statistical_forecastingworker--loglevel=INFO >.

反射(reflect)下面的评论也可以作为:

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

关于python - VSCode 调试 Celery Worker,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57707031/

45 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com