gpt4 book ai didi

python - 有没有办法在 Azure Functions 中延迟 QueueMessage?

转载 作者:行者123 更新时间:2023-12-03 02:46:18 25 4
gpt4 key购买 nike

我需要向队列存储提交一条消息,但不让它立即触发绑定(bind)到该队列的函数。在这个基本示例中,我想向队列提交一条消息,以便它在 1 分钟后触发一个函数:

def main(msg: func.QueueMessage, outputQueueItem: func.Out[func.QueueMessage]) -> None:
data = msg.get_json()

# Do some fancy stuff

message = func.QueueMessage(body=json.dumps({"spam": 1, "eggs": "ham"}))

# This causes an AttributeError since you cant set time_next_visible
message.time_next_visible = datetime.utcnow() + timedelta(minutes=1)

outputQueueItem.set(message)

# More fancy stuff down here

我可以在提交消息之前添加一个 time.sleep(60) ,但这似乎是不好的做法,而且它会延迟提交消息行下方的任何其他代码并增加计费运行该函数的成本。如果需要很大的延迟(例如一个小时),这尤其糟糕。

有什么好的办法可以延迟消息立即触发下一个函数吗?

最佳答案

Python 无法使用持久函数,因此你有两种方法来实现你的想法。

首先正如你所说,使用 sleep 让它等待。

第二个来自Python Azure SDK,使用put_message方法使消息保留在队列中(设置可见性),看看如何使用put_message .

关于python - 有没有办法在 Azure Functions 中延迟 QueueMessage?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58649298/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com