为什么我的 python 计时器触发功能没有在正确的时间运行?
问题描述
这可能是一个菜鸟问题.我有一个响应 HTTP 请求的 Azure 函数,它工作正常,我可以从浏览器或 Python 3.8 脚本调用它.
this is probably a noob question. I have an Azure Function that responds to HTTP requests and it works fine, I can call it from a browser or from a Python 3.8 script.
我想制作另一个具有 Timer Trigger
的函数,并按计划调用 HTTP 触发器函数
.
I want to make another function that will have Timer Trigger
and will call the HTTP trigger function
on a schedule.
HTTP 触发函数
返回一个带有执行结果的简单字符串.
HTTP Trigger function
returns a simple string with execution results.
现在我的 Timer trigger function
代码使用 Python Requests
并且每次都在本地工作,但部署到 Azure 时只能工作 1/10 次.有时它会在超时 30 分钟时返回错误.整个过程最多只能运行 1-2 分钟,所以我不明白它卡在哪里了.
Now my code for Timer trigger function
is using Python Requests
and it works locally every time, but will work only 1/10 times when deployed to Azure. Other times it returns error when it reaches timeout of 30 minutes. The whole thing should run only for 1-2 minutes max so I don't understand where it gets stuck.
成功时它可以工作(我可以在 HTTP 触发脚本
的后端看到),但在 azure 日志中,记录器保存 404 错误
页面 html 而不是 HTTP 触发函数
应该返回.
When successful it works(I can see in backend of HTTP trigger script
), but in azure logs the logger saves 404 error
page html instead of the string that HTTP trigger function
should return.
这是定时器触发功能的代码:
Here is the code for Timer Trigger function:
import datetime
import logging
import azure.functions as func
import requests
def main(mytimer: func.TimerRequest) -> None:
URL = "https://rob-functions.azurewebsites.net/api/ss_kite_scrape_http"
r = requests.get(url = URL)
data = r.text
logging.info(f'TIMER TRIGGER HAS RUN. RESULT:{data}')
如何解决或解决此问题?日志记录问题并不那么重要,但必须以某种方式解决超时问题,我不知道从哪里开始,因为它在本地完美运行.
How to troubleshoot or fix this? The logging issue is not so important but the timeout issue has to be fixed somehow and I have no idea where to start since it works perfectly locally.
解决方案
根据一些测试,我遇到了和你类似的问题.我使用默认代码创建了一个 HttpTrigger(我在其中添加了一行 time.sleep(20)
).然后我创建一个带有 requests
模块的 TimerTrigger(cron 表达式是 0 */1 * * * *
)来调用 HttpTrigger 函数.这两个功能在一个功能应用程序中,似乎问题是由两个功能相互交互引起的,但我不知道为什么.这两个函数的代码看起来都很好.
According to some test, I met the issue similar with yours'. I create a HttpTrigger with the default code(I add a line time.sleep(20)
in it). And then I create a TimerTrigger(the cron expression is 0 */1 * * * *
) with requests
module to call the HttpTrigger function. The two functions are in one function app, it seems the issue was caused by the two functions interact with each other but I don't know why. All of the two functions code looks fine.
作为一种解决方法,我在不同的函数应用程序中创建了两个函数(HttpTrigger 和 TimerTrigger),并将它们部署到 azure 中的两个函数应用程序.然后它工作正常,它们不会相互交互.
For a workaround, I create the two functions(HttpTrigger and TimerTrigger) in different function apps and deploy them to two function app in azure. Then it works fine, they will not interact with each other.
希望对你有帮助~
相关文章