在Azure WebApp上运行后台计划任务(Python)

vzgqcmou  于 4个月前  发布在  Python
关注(0)|答案(1)|浏览(79)

我有一个Azure Web App,目前运行在Linux环境中。我有一个Python脚本,需要每4小时在后台运行一次。此脚本使用外部模块,如pysftp,pandas等。需要注意的是,此脚本不是API;这是一个完全在后台运行的数据转换脚本。我如何设置每4小时在后台执行此脚本?
我尝试过使用Python调度模块和crontab,因为我们处理的是Linux系统,但到目前为止,我在这两种情况下都没有取得成功的结果

5m1hhzi4

5m1hhzi41#

我使用下面的计时器触发azure函数,它调用下面的脚本,并遵循这个SO-thread

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文件中添加包:


相关问题