我有一个当前在 Linux 环境中运行的 Azure Web 应用程序。我有一个 Python 脚本,需要每 4 小时在后台运行一次。该脚本使用外部模块,例如 pysftp、pandas 等。需要注意的是,该脚本不是 API;而是一个 API。它是一个完全在后端运行的数据转换脚本。如何设置该脚本每 4 小时在后台执行一次?
我尝试过使用Python调度模块和crontab,因为我们正在处理Linux系统,但到目前为止,我在这两种情况下都没有取得成功的结果
我使用下面的计时器触发azure函数,该函数调用如下所示的脚本并遵循此SO线程:
import datetime
import logging
from pkg_resources import run_script
import sys
import os
import importlib.util as iu
import azure.functions as func
def main(mytimer: func.TimerRequest) -> None:
utc_timestamp = datetime.datetime.utcnow().replace(
tzinfo=datetime.timezone.utc).isoformat()
rithwik_script('test')
if mytimer.past_due:
logging.info('The timer is past due!')
logging.info('Python timer trigger function ran at %s', utc_timestamp)
def rithwik_script(run_script):
spath = os.path.join(os.path.dirname(__file__), f'{run_script}.py')
s = iu.spec_from_file_location(run_script, spath)
m = iu.module_from_spec(s)
sys.modules[s.name] = m
s.loader.exec_module(m)
要执行的脚本(test.py):
print("Hello Rithwik Bojja")
Output:
您可以在requirements.txt文件中添加包: