python - 无法在函数应用中向 Azure 队列添加多个项目

标签 python azure azure-functions queuetrigger

我有一个 TimerTrigger 函数,它每隔几分钟运行一次,并将多个项目添加到队列中,QueueTrigger 函数应该处理这些项目。但每次 QueueTrigger 函数仅触发一次。

我的 TimerTrigger 函数:

def main(mytimer: func.TimerRequest, msg: func.Out[str]) -> None:
    utc_timestamp = datetime.datetime.utcnow().replace(
        tzinfo=datetime.timezone.utc).isoformat()

    if mytimer.past_due:
        logging.info('The timer is past due!')

    logging.info('Python timer trigger function ran at %s', utc_timestamp)
    msg.set("1")
    msg.set("2")

队列触发函数:

def main(msg: func.QueueMessage) -> None:
    logging.info('Python queue trigger function processed a queue item: %s',
                 msg.get_body().decode('utf-8'))

TimerTrigger 的 function.json:

{
  "scriptFile": "__init__.py",
  "bindings": [
    {
      "name": "mytimer",
      "type": "timerTrigger",
      "direction": "in",
      "schedule": "0 */2 * * * *"
    },
    {
      "name": "msg",
      "type": "queue",
      "direction": "out",
      "queueName": "js-queue-items",
      "connection": "AzureWebJobsStorage"
    }
  ]
}

QueueTrigger 的 function.json:

{
  "scriptFile": "__init__.py",
  "bindings": [
    {
      "name": "msg",
      "type": "queueTrigger",
      "direction": "in",
      "queueName": "js-queue-items",
      "connection": "AzureWebJobsStorage"
    }
  ]
}

local.settings.json:

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "FUNCTIONS_WORKER_RUNTIME": "python"
  }
}

我该如何解决这个问题?

最佳答案

没有使用 azure 时间触发器连接到队列存储的固有方法,即队列存储会将时间触发器视为任何其他控制台应用程序或尝试连接到存储并添加消息的任何其他程序。

因此,我们必须使用连接字符串以经典方式添加消息。

connectionstring = ""
queuename = ""    
messages = ""
queueClient = QueueClient.from_connection_string(connectionString, queueName)
queueClient.send_message(message)

因此,当队列触发器在启动过程中只会启动一次,并且由于没有添加消息而保持空闲状态,因此不会被触发。

请参阅 timetrigger 上的文档和 queuetriggerhow to add message to queue using python .

关于python - 无法在函数应用中向 Azure 队列添加多个项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72072984/

相关文章:

c# - 在 azure-pipelines.yml 中为 dotnetpublish 命令转义空格

Azure Functions,nuget 包安装失败

python - 获取错误 530 5.7.0 使用 Django Python 发送电子邮件时需要身份验证

python - 在 python 中调试到库

python - 如何在 Python 2.7 中加载 Python 3 pickle *不改变它*?

python - 如何在 pygame 中循环播放音频文件的一部分?

c# - Azure应用程序功能(功能2.x): Create new document in CosmosDB via HTTP function

azure - 如何获取存储在 Azure 资源管理器中的捕获的 VM 镜像的 URL?

powershell - Azure Function Apps - PowerShell 的 ErrorActionPreference

azure - 如何修复发布 Azure Functions 时出现的 "Validate Connection"错误“找不到 testMsDeployConnection”?