本文介绍了计时器触发器不会触发队列,但手动触发器会触发-Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个队列触发器,当将消息手动添加到队列中时,它会启动并按预期运行.但是,通过以下计时器触发功能将消息写入队列时,它将无法启动.我可以看到该消息已被触发器成功写入.
I have a queue trigger which when message is manually added into queue it gets started and runs as expected. However, when message is written into queue by the following timer trigger function it fails to start. I can see the message is successfully written by the trigger.
** init .py **
** 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编码有关.我添加了三行代码:
According to some test, the problem is related to base64 encode. I added three lines of code:
message_bytes = message.encode('ascii')
base64_bytes = base64.b64encode(message_bytes)
base64_message = base64_bytes.decode('ascii')
请参考以下整个功能代码:
Please refer to the whole function code below:
host.json
host.json
{
"version": "2.0",
"logging": {
"applicationInsights": {
"samplingSettings": {
"isEnabled": true,
"excludedTypes": "Request"
}
}
},
"extensionBundle": {
"id": "Microsoft.Azure.Functions.ExtensionBundle",
"version": "[1.*, 2.0.0)"
}
}
function.json
function.json
{
"scriptFile": "__init__.py",
"bindings": [
{
"name": "mytimer",
"type": "timerTrigger",
"direction": "in",
"schedule": "*/20 * * * * *"
}
]
}
这篇关于计时器触发器不会触发队列,但手动触发器会触发-Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!