【问题标题】:How can I set Queue Storage message TTL in the context of an Azure Function output binding in Python?如何在 Python 中的 Azure 函数输出绑定的上下文中设置队列存储消息 TTL?
【发布时间】:2020-01-16 21:26:40
【问题描述】:

我有一个带有队列输出绑定的 python azure 函数。我成功地使用此绑定从函数内对消息进行排队。是否可以在底层队列或消息本身上设置消息 TTL?我不需要在每条消息的基础上设置它,但如果这是唯一的选择,我会这样做。

host.json

{
  "version": "2.0",
  "extensionBundle": {
    "id": "Microsoft.Azure.Functions.ExtensionBundle",
    "version": "[1.*, 2.0.0)"
  }
}

函数.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": "predictions",
      "connection": "AzureWebJobsStorage"
    }
  ]
}

功能代码

import json
import logging
import azure.functions as func
from graphene import Schema
from .helpers import responses
from .schema.Query import Query

def main(req: func.HttpRequest, msg: func.Out[func.QueueMessage]) -> func.HttpResponse:
    logging.info('Executing GraphQL function.')

    try:
        query = req.get_body().decode()
    except ValueError:
        pass

    if query:
        schema = Schema(Query)
        results = schema.execute(query)
        response = responses.graphql(results)

        # Write response to azure queue storage
        message = responses.storage(query, response)
        if message:
            msg.set(message)

        return response
    else:
        return responses.bad_request(
            'Please pass a GraphQL query in the request body.')

【问题讨论】:

    标签: python azure-functions azure-queues


    【解决方案1】:

    目前只支持c#语言,可以绑定CloudQueue类型。如果是其他语言,则必须使用SDK方法来实现。如果您坚持使用此功能,您可以到此github issue 评论您的要求。

    下面是我用azure-storage-queue 2.1.0在HTTP触发函数中设置TTL的测试代码。

    import logging
    import azure.functions as func
    from azure.storage.queue import QueueService
    import os
    
    def main(req: func.HttpRequest) -> func.HttpResponse:
        logging.info('Python HTTP trigger function processed a request.')
        queue_service = QueueService(connection_string=os.environ['AzureWebJobsStorage'])
    
        message = req.params.get('message')
        if not message:
            try:
                req_body = req.get_json()
            except ValueError:
                pass
            else:
                message = req_body.get('message')
    
        if message:
            queue_service.put_message('myqueue',message,None,300,None)
            return func.HttpResponse(f" {message}!")
        else:
            return func.HttpResponse(
                 "Please pass message in the request body",
                 status_code=400
            )
    

    【讨论】:

    • 谢谢乔治,这很好用。我已经在考虑将我的函数应用程序移植到 C#,所以我不会在 Github 上推送 Python 的问题。
    猜你喜欢
    • 2021-10-03
    • 2017-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-04
    • 1970-01-01
    • 2017-09-29
    • 1970-01-01
    相关资源
    最近更新 更多