python-3.x - 定时器触发不会触发队列但手动录入会触发-Python

标签 python-3.x azure-functions timer-trigger queuetrigger

我有一个队列触发器,当消息手动添加到队列中时,它会启动并按预期运行。但是,当消息通过以下定时器触发函数写入队列时,它无法启动。我可以看到触发器已成功写入消息。

** init.py**

import datetime
import logging
import os, uuid
from azure.storage.queue import (
        QueueClient,
        BinaryBase64EncodePolicy,
        BinaryBase64DecodePolicy
)

import os, uuid
import azure.functions as func


def main(mytimer: func.TimerRequest) -> None:
    utc_timestamp = datetime.datetime.utcnow().replace(
        tzinfo=datetime.timezone.utc).isoformat()
    conn_str = os.environ['AzureWebJobsStorage']

    queue_name="outqueue12"
    
    message = u"Hello234"
    queue_client = QueueClient.from_connection_string(conn_str, queue_name)
    # Setup Base64 encoding and decoding functions
    queue_client.message_encode_policy = BinaryBase64EncodePolicy()
    queue_client.message_decode_policy = BinaryBase64DecodePolicy()
    
    queue_client.send_message(message)
    
           
    logging.info('Python timer trigger function ran at %s', utc_timestamp)

host.json

{
  "version": "2.0",
  "logging": {
    "applicationInsights": {
      "samplingSettings": {
        "isEnabled": true,
        "excludedTypes": "Request"
      }
    }
  },
  "extensionBundle": {
    "id": "Microsoft.Azure.Functions.ExtensionBundle",
    "version": "[1.*, 2.0.0)"
  }
}

我错过了什么吗?

最佳答案

根据测试,该问题与base64编码有关。我添加了三行代码:

message_bytes = message.encode('ascii')
base64_bytes = base64.b64encode(message_bytes)
base64_message = base64_bytes.decode('ascii')

完整函数代码请引用下面: enter image description here

主机.json

{
  "version": "2.0",
  "logging": {
    "applicationInsights": {
      "samplingSettings": {
        "isEnabled": true,
        "excludedTypes": "Request"
      }
    }
  },
  "extensionBundle": {
    "id": "Microsoft.Azure.Functions.ExtensionBundle",
    "version": "[1.*, 2.0.0)"
  }
}

函数.json

{
  "scriptFile": "__init__.py",
  "bindings": [
    {
      "name": "mytimer",
      "type": "timerTrigger",
      "direction": "in",
      "schedule": "*/20 * * * * *"
    }
  ]
}

关于python-3.x - 定时器触发不会触发队列但手动录入会触发-Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65624035/

相关文章:

python - 如何使用 QTableWidget PageUp/PageDown 表格?

.net-core - 无法加载文件或程序集 Microsoft.Extensions.Logging.Abstractions

Azure 函数未按计划时间触发

azure - 计时器触发器 Azure Function 停止工作,没有任何更改

Python 3.2 - 连接和字符串格式化行为不符合预期

python - 打印链表队列中的元素

python - 从列表中删除包含 (",") 的所有元素

azure - 对 Azure Function App 实现 Application Insight

javascript - 如何在 JavaScript Azure Functions 中共享代码?

azure - Azure Function Apps 中全局变量的替代方案是什么?