gpt4 book ai didi

python - 在 VS Code 中本地调试多个 Python Azure 函数

转载 作者:行者123 更新时间:2023-12-04 10:12:49 27 4
gpt4 key购买 nike

我关注了 this Microsoft使用 CLI 在 python 中创建 Azure-Function 的教程。

然后我创建了第二个函数,都是 HttpTriggered。

C:\Users\rnwol\workspace\anotherazfunc
├── host.json
└── LocalFunctionProj
├── host.json
├── HttpCONTACT
| ├── function.json
| ├── host.json
| ├── __init__.py
| └── __pycache__
├── HttpRNWOLF
| ├── function.json
| ├── __init__.py
| └── __pycache__
├── local.settings.json
└── requirements.txt

在 Powershell 终端中,我可以按如下方式运行这两个功能...
(.venv) C:\Users\rnwol\workspace\anotherazfunc\LocalFunctionProj> func host start
Found Python version 3.8.2 (python).

%%%%%%
%%%%%%
@ %%%%%% @
@@ %%%%%% @@
@@@ %%%%%%%%%%% @@@
@@ %%%%%%%%%% @@
@@ %%%% @@
@@ %%% @@
@@ %% @@
%%
%

Azure Functions Core Tools (3.0.2245 Commit hash: 1d094e2f3ef79b9a478a1621ea7ec3f93ac1910d)
Function Runtime Version: 3.0.13139.0
[15/04/2020 20:43:27] FUNCTIONS_WORKER_RUNTIME set to python. Skipping WorkerConfig for language:java
[15/04/2020 20:43:27] FUNCTIONS_WORKER_RUNTIME set to python. Skipping WorkerConfig for language:node
[15/04/2020 20:43:27] FUNCTIONS_WORKER_RUNTIME set to python. Skipping WorkerConfig for language:powershell
[15/04/2020 20:43:28] Building host: startup suppressed: 'False', configuration suppressed: 'False', startup operation id: '21466b21-c6d0-4a25-9df4-61b42b364bef'
[15/04/2020 20:43:28] Reading host configuration file 'C:\Users\rnwol\workspace\anotherazfunc\LocalFunctionProj\host.json'
[15/04/2020 20:43:28] Host configuration file read:
[15/04/2020 20:43:28] {
[15/04/2020 20:43:28] "version": "2.0",
[15/04/2020 20:43:28] "extensionBundle": {
[15/04/2020 20:43:28] "id": "Microsoft.Azure.Functions.ExtensionBundle",
[15/04/2020 20:43:28] "version": "[1.*, 2.0.0)"
[15/04/2020 20:43:28] }
[15/04/2020 20:43:28] }
[15/04/2020 20:43:28] Reading functions metadata
[15/04/2020 20:43:28] 2 functions found
...

我可以根据需要使用 httpie 来触发这两个功能。

问题 - 如何在 VS-Code 中进行交互式调试?

当我现在在 VS Code 中打开文件时,对于单个函数,然后我可以按 F5 运行命令来调试和单步执行触发器事件并找出问题所在。

我应该使用什么配置来运行这两个功能,以便我可以在 VS-Code 中以交互方式设置断点和调试?我正在使用 Windows 10。

解决方向

我已经配置了一个示例模板 github repo https://github.com/rnwolf/azure-func-python-layout结合我的研究结果,您可以将其用作 Azure Functions 的入门。

对我有用的布局如下。
在下面的示例中,确保在根目录 multi3 中打开 VSCode。
我发现你可以删除 .vscode 和 .vstest 然后 VSCode 似乎弄清楚如何设置一些有效的默认值。
(.venv) ...orkspace/multi3$ tree -L 3 -a
.
├── .coverage
├── .coveragerc
├── .flake8
├── .git
├── .github
│ └── workflows
│ └── main.yml
├── .gitignore
├── .hypothesis
├── .pre-commit-config.yaml
├── .pylintrc
├── .python-version
├── .venv
│ ├── COPYING
│ ├── COPYING.GPL
│ ├── bin
│ │ ├── Activate.ps1
│ │ ├── activate
│ │ ├── activate.csh
│ │ ├── activate.fish
│ │ ├── autopep8-wrapper
│ │ ├── bandit
│ │ ├── bandit-baseline
│ │ ├── bandit-config-generator
│ │ ├── black
│ │ ├── blackd
│ │ ├── chardetect
│ │ ├── check-added-large-files
│ │ ├── check-ast
│ │ ├── check-builtin-literals
│ │ ├── check-byte-order-marker
│ │ ├── check-case-conflict
│ │ ├── check-docstring-first
│ │ ├── check-executables-have-shebangs
│ │ ├── check-json
│ │ ├── check-merge-conflict
│ │ ├── check-symlinks
│ │ ├── check-toml
│ │ ├── check-vcs-permalinks
│ │ ├── check-xml
│ │ ├── check-yaml
│ │ ├── coverage
│ │ ├── coverage-3.8
│ │ ├── coverage3
│ │ ├── debug-statement-hook
│ │ ├── detect-aws-credentials
│ │ ├── detect-private-key
│ │ ├── dmypy
│ │ ├── double-quote-string-fixer
│ │ ├── easy_install
│ │ ├── easy_install-3.8
│ │ ├── end-of-file-fixer
│ │ ├── epylint
│ │ ├── file-contents-sorter
│ │ ├── fix-encoding-pragma
│ │ ├── flake8
│ │ ├── forbid-new-submodules
│ │ ├── http
│ │ ├── https
│ │ ├── identify-cli
│ │ ├── isort
│ │ ├── mixed-line-ending
│ │ ├── mypy
│ │ ├── mypyc
│ │ ├── name-tests-test
│ │ ├── no-commit-to-branch
│ │ ├── nodeenv
│ │ ├── pbr
│ │ ├── pip
│ │ ├── pip-compile
│ │ ├── pip-sync
│ │ ├── pip3
│ │ ├── pip3.8
│ │ ├── pipenv
│ │ ├── pipenv-resolver
│ │ ├── pre-commit
│ │ ├── pre-commit-validate-config
│ │ ├── pre-commit-validate-manifest
│ │ ├── pretty-format-json
│ │ ├── py.test
│ │ ├── pycodestyle
│ │ ├── pydocstyle
│ │ ├── pyflakes
│ │ ├── pygmentize
│ │ ├── pylint
│ │ ├── pyreverse
│ │ ├── pytest
│ │ ├── python -> /home/rnwolf/.pyenv/versions/3.8.2/bin/python
│ │ ├── python3 -> python
│ │ ├── requirements-txt-fixer
│ │ ├── safety
│ │ ├── sort-simple-yaml
│ │ ├── stubgen
│ │ ├── stubtest
│ │ ├── symilar
│ │ ├── trailing-whitespace-fixer
│ │ ├── virtualenv
│ │ └── virtualenv-clone
│ ├── include
│ ├── lib
│ │ └── python3.8
│ ├── lib64 -> lib
│ └── pyvenv.cfg
├── .vscode
│ ├── .ropeproject
│ │ └── config.py
│ ├── extensions.json
│ ├── launch.json
│ ├── settings.json
│ └── tasks.json
├── .vstest
│ ├── extensions.json
│ ├── launch.json
│ ├── settings.json
│ └── tasks.json
├── Create .venv pythonvirtual env here.txt
├── LICENSE.md
├── README.md
├── __app__
│ ├── .funcignore
│ ├── .python_packages
│ ├── __init__.py
│ ├── conftest.py
│ ├── host.json
│ ├── http_trigger_1
│ │ ├── __init__.py
│ │ ├── __pycache__
│ │ └── function.json
│ ├── http_trigger_2
│ │ ├── __init__.py
│ │ ├── __pycache__
│ │ └── function.json
│ ├── local.settings.SAMPLE.json
│ ├── requirements.txt
│ └── sharedcode
│ ├── __init__.py
│ ├── __pycache__
│ └── my_helper_functions.py
├── dev-requirements.in
├── dev-requirements.txt
├── mypy.ini
├── pytest.ini
└── tests
├── .pylintrc
├── __init__.py
├── testHttpTrigger1.http
├── testHttpTrigger2.http
├── test_http_trigger_1.py
└── test_http_trigger_2.py

最佳答案

在您的情况下,运行 f5 将启动两个功能。调试这两个函数时需要向对应的URL发送请求才能进入逻辑。在functionapp级别使用f5时,所有的trigger都会启动,处于等待trigger的状态。

您可以在需要调试的触发逻辑中创建一些断点,然后满足其触发方式。

enter image description here

例如,如果您有两个 http 触发器,并且要调试 httptrigger1。您可以在其中创建一些断点,然后运行 ​​f5。之后,向相关url发送请求,然后使用f8(continue)进行调试。

这是我的launch.json:

{
"version": "0.2.0",
"configurations": [
{
"name": "Attach to Python Functions",
"type": "python",
"request": "attach",
"port": 9091,
"preLaunchTask": "func: host start"
}
]
}

这是settings.json:
{
"azureFunctions.deploySubpath": ".",
"azureFunctions.scmDoBuildDuringDeployment": true,
"azureFunctions.pythonVenv": ".venv",
"azureFunctions.projectLanguage": "Python",
"azureFunctions.projectRuntime": "~2",
"debug.internalConsoleOptions": "neverOpen",
"python.pythonPath": ".venv\\Scripts\\python.exe"
}

这是我的tasks.json:
{
"version": "2.0.0",
"tasks": [
{
"type": "func",
"command": "host start",
"problemMatcher": "$func-watch",
"isBackground": true,
"dependsOn": "pipInstall"
},
{
"label": "pipInstall",
"type": "shell",
"osx": {
"command": "${config:azureFunctions.pythonVenv}/bin/python -m pip install -r requirements.txt"
},
"windows": {
"command": "${config:azureFunctions.pythonVenv}\\Scripts\\python -m pip install -r requirements.txt"
},
"linux": {
"command": "${config:azureFunctions.pythonVenv}/bin/python -m pip install -r requirements.txt"
},
"problemMatcher": []
}
]
}

关于python - 在 VS Code 中本地调试多个 Python Azure 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61238556/

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