为什么我的python计时器触发功能未在正确的时间运行? [英] Why is my python timer trigger function not running at the correct time?

查看:82
本文介绍了为什么我的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触发功能的代码正在使用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触发脚本的后端中看到),但是在天蓝色的日志中,记录器保存了 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),然后将它们以天蓝色部署到两个功能应用程序中.这样就可以了,它们不会互相影响.

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.

希望有帮助〜

这篇关于为什么我的python计时器触发功能未在正确的时间运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆