python - 如何在 Azure 上每 24 小时自动运行一次我的 python 脚本?
问题描述
我有一个 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")
推荐阅读
- sql - 具有滞后事件分区的行号
- gitlab-ci - gitlab:有没有办法在工作期间而不是之后http访问工件?
- html - 如何使用 PWA 在其中一个页面中禁用手机滚动
- session - Puppeteer:如何存储会话(包括 cookie、页面状态、本地存储等)并稍后继续?
- apache-kafka - 即使生产者得到确认,Kafka中也会发生消息丢失吗?
- c# - 如何将参数值从视图传递到控制器?
- cython - 如何修复'AttributeError:类型对象'pyfmi.fmi.ModelBase'没有属性'__reduce_cython__''
- asterisk-java - 发送命令操作时出错
- mysql - Laravel-从下拉列表中选择值后自动填充输入
- typescript - 为什么 typescript 版本与 Windows 10 上的全局安装版本不同?