【发布时间】:2021-04-04 08:47:04
【问题描述】:
我无法理解发生了什么。 我从字面上遵循所有 Microsoft 文档,实际上甚至不使用我自己的任何脚本/代码。 首先,我按照他们的文档创建 Python 函数。有效。 https://docs.microsoft.com/en-us/azure/azure-functions/create-first-function-cli-python?tabs=azure-cli%2Ccmd%2Cbrowser 使用命令行工具将 Azure Functions 连接到 Azure 存储的第二个文档。不可重现。 https://docs.microsoft.com/en-us/azure/azure-functions/functions-add-output-binding-storage-queue-cli?pivots=programming-language-python&tabs=bash%2Cbrowser 我真的按照每一步,但收到错误。
更令人惊讶的是,他们最终向我展示了与第一篇文章不同的代码。我尝试了两个版本--没有一个工作。
这些是他们文档中的代码。 这是他们的 python 脚本代码 (init.py)
import logging
import azure.functions as func
def main(req: func.HttpRequest, msg: func.Out[func.QueueMessage]) -> str:
name = req.params.get('name')
if not name:
try:
req_body = req.get_json()
except ValueError:
pass
else:
name = req_body.get('name')
if name:
msg.set(name)
return func.HttpResponse(f"Hello {name}!")
else:
return func.HttpResponse(
"Please pass a name on the query string or in the request body",
status_code=400
)
这是 JSON 函数代码:
{
"scriptFile": "__init__.py",
"bindings": [
{
"authLevel": "anonymous",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"methods": [
"get",
"post"
]
},
{
"type": "http",
"direction": "out",
"name": "$return"
},
{
"type": "queue",
"direction": "out",
"name": "msg",
"queueName": "outqueue",
"connection": "AzureWebJobsStorage"
}
]
}
由于他们没有发布完整版本的代码,我添加了诸如括号之类的内容。如果您想要文档引用,那就是他们所说的:
虽然一个函数只能有一个触发器,但它可以有多个输入和输出绑定,这让您无需编写自定义集成代码即可连接到其他 Azure 服务和资源。 您在函数文件夹中的 function.json 文件中声明这些绑定。在上一个快速入门中,HttpExample 文件夹中的 function.json 文件在 bindings 集合中包含两个绑定:
"scriptFile": "__init__.py",
"bindings": [
{
"authLevel": "function",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"methods": [
"get",
"post"
]
},
{
"type": "http",
"direction": "out",
"name": "$return"
}
每个绑定至少有一个类型、一个方向和一个名称。在上面的示例中,第一个绑定的类型为 httpTrigger,方向为 in。对于 in 方向,name 指定触发器调用时发送到函数的输入参数的名称。
集合中的第二个绑定是 http 类型,方向为 out,在这种情况下,$return 的特殊名称表明此绑定使用函数的返回值,而不是提供输入参数。
要从此函数写入 Azure 存储队列,请添加一个名为 msg 的队列类型的输出绑定,如下面的代码所示:
"bindings": [
{
"authLevel": "anonymous",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"methods": [
"get",
"post"
]
},
{
"type": "http",
"direction": "out",
"name": "$return"
},
{
"type": "queue",
"direction": "out",
"name": "msg",
"queueName": "outqueue",
"connection": "AzureWebJobsStorage"
}
]
在这种情况下,将 msg 作为输出参数提供给函数。对于队列类型,您还必须在 queueName 中指定队列的名称,并在连接中提供 Azure 存储连接的名称(来自 local.settings.json)。
因此,即使此代码已发布在文档中,它似乎也无法正常工作。或者他们的 python 代码不工作。我不知道。
我试图按照他们的步骤进行操作。 我还尝试复制粘贴他们的新版本代码(与原始版本略有不同) 但没有任何效果 我仍然收到此错误(我更改了一些我怀疑是敏感的元素)
(.venv) C:\Users\usr\LocalFunctionProj>func start
Found Python version 3.8.5 (py).
Azure Functions Core Tools
Core Tools Version: 3.0.3160 Commit hash: 00aa7f49cc5c5f15241a5e6e5363256f19ceb980
Function Runtime Version: 3.0.14916.0
Functions:
HttpExample: [GET,POST] http://localhost:8072/api/HttpExample
For detailed output, run func with --verbose flag.
[2020-12-27T08:45:11.912Z] Worker process started and initialized.
[2020-12-27T08:45:12.048Z] Worker failed to function id ebece17c-3077-4f78-bcca-d46565cef86c.
[2020-12-27T08:45:12.050Z] Result: Failure
Exception: FunctionLoadError: cannot load the HttpExample function: the following parameters are declared in Python but not in function.json: {'msg'}
Stack: File "D:\Program Files\Microsoft\Azure Functions Core Tools\workers\python\3.8\WINDOWS\X64\azure_functions_worker\dispatcher.py", line 272, in _handle__function_load_request
self._functions.add_function(
File "D:\Program Files\Microsoft\Azure Functions Core Tools\workers\python\3.8\WINDOWS\X64\azure_functions_worker\functions.py", line 112, in add_function
raise FunctionLoadError(
.
[2020-12-27T08:45:16.457Z] Host lock lease acquired by instance ID '000000000000000000000000AF616381'.
我真的无法理解他们自己的代码如何不起作用。我只想学习如何将我的 python 脚本部署到 Azure,没想到会是这么大的挑战。
这是我更新后的 python init.py 代码在示例答案后的样子:
这是示例答案后更新后的 function.json 代码的样子:
【问题讨论】:
-
评论不用于扩展讨论;这个对话是moved to chat。
标签: python azure azure-functions