繁体   English   中英

如何在 Azure 上每 24 小时自动运行一次我的 python 脚本?

[英]How to run my python script automatically every 24 hours on Azure?

我有一个 python 脚本(用 Jupyter notebook 编写),我想在 Azure 中运行这个脚本。 python 脚本基本上从 API 源(每 24 小时更新一次)获取数据并更新 Azure 的 SQL 数据库。 所以这个自动化的python脚本会在运行时更新数据库表

有人可以取悦我吗?

下面是我写的python代码,

import pyodbc
import requests
import json 
import pandas as pd

responses = requests.get("https://data.buffalony.gov/resource/d6g9-xbgu.json")

crime_data = json.loads(responses.text)

dic = {}

dic = crime_data

df = pd.DataFrame.from_dict(dic)

dff = df[['case_number','day_of_week','incident_datetime','incident_description','incident_id','incident_type_primary']].copy()

connection = pyodbc.connect ('Driver={ODBC Driver 17 for SQL Server};Server=servername;Database=Databasename;UID=admin;PWD=admin')

cur = connection.cursor()

row = []

for i in range(dff.shape[0]):

   row.append(dff.iloc[i].tolist())

sql = '''\
INSERT INTO [dbo].[FF] ([case_number],[day_of_week],[incident_datetime],[incident_description],[incident_id],[incident_type_primary]) values (?,?,?,?,?,?)
'''

for i in range(dff.shape[0]):

   cur.execute(sql,row[i])

connection.commit()

我不使用 azure 和 jupyter notebook,但我想我有一个解决方案

import time
import pyodbc
import requests
import json 
import pandas as pd
while 1:
    responses = requests.get("https://data.buffalony.gov/resource/d6g9-xbgu.json")

    crime_data = json.loads(responses.text)

    dic = {}

    dic = crime_data

    df = pd.DataFrame.from_dict(dic)

    dff =  df    [['case_number','day_of_week','incident_datetime','incident_description','incident_i         d','incident_type_primary']].copy()

    connection = pyodbc.connect ('Driver={ODBC Driver 17 for SQL Server};Server=servername;Database=Databasename;UID=admin;PWD=admin')

    cur = connection.cursor()

    row = []

    for i in range(dff.shape[0]):
        row.append(dff.iloc[i].tolist())

    sql = '''\
    INSERT INTO [dbo].[FF] ([case_number],[day_of_week],[incident_datetime],    [incident_description],[incident_id],[incident_type_primary]) values (?,?,?,?,?,?)
    '''

    for i in range(dff.shape[0]):

        cur.execute(sql,row[i])

    connection.commit()
    time.sleep(86400)

如果没有在启动文件中创建一个新的python程序,如下所示:

import time, os
while 1:
    if time.ctime()[11:13] >= "update hour" and time.ctime()[0:4] != open("path/to/any_file.txt").read():
        file = open("path/to/any_file.txt", "w")
        file.write(time.ctime()[0:4])
        file.close()
        os.system("python /path/to/file.py")

Azure WebJobs 之类的任务计划程序会为您执行此操作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM