Flask Route: return JSON 同时写入数据库
Flask Route: return JSON and write to database simultaneously
我是 Flask 的新手,所以也许我的方法是错误的,但我使用一个路径写入 sqlite 数据库和 return JSON 然后显示在 table .该路由按预期工作,但它在用户端相当慢,因为写入数据库发生在 JSON 被 returned 之前。写入数据库需要 0.1-0.4 秒,所以这不是一个大问题,但如果我能消除这种延迟就更好了。
下面是我的路线。我删除了 new_entry 中的列名,但代码再次按预期运行,只是定时部分比预期的要慢。
注意:我尝试使用 add_all 进行批量插入,但在我测试时它比 for 循环慢。
@app.route('/api/data/<string:from_port>/<string:to_port>/')
def data(from_port,to_port):
# this calls the cargosmart sailing schedule api and converts the json return into the desired format
api_url = helpers.get_api_url(int(un_to_int_dict[from_port]),int(un_to_int_dict[to_port]), api_key=SECRETS.get("big_schedules_api_key"))
df = helpers.create_df(requests.get(url = api_url
, proxies=SECRETS.get("proxies")
),scac_dict = scac_dict)
bigitems = [helpers.create_big_item(df[df['Route ID']==i]) for i in df['Route ID'].unique()]
api_results = {"data":[x.to_dict() for x in bigitems]}
origin_port = from_port
destination_port = to_port
current_date = date.today().strftime("%m/%d/%Y")
new_user_entry = user_data(ip_address = request.remote_addr,
origin_port = origin_port,destination_port = destination_port,date = current_date)
db.session.add(new_user_entry)
request_id = max([x[0] for x in db.session.query(user_data.id).all()])
new_sailing_entry = [cargosmart_sailings(
from_port = df['From Port'].iloc[i],to_port= df['To Port'].iloc[i],terminal = df['To Terminal'].iloc[i],
etd = convert_date(df['ETD'].iloc[i]),
eta = convert_date(df['ETA'].iloc[i]),
vessel_name = df['Vessel Name'].iloc[i],service= df['Service'].iloc[i],
external_voyage_number = df['External Voyage Number'].iloc[i],
direct = df['Direct'].iloc[i],carrier = df['Carrier'].iloc[i],
cutoff = convert_date(df['Cutoff'].iloc[i]),
transit_time = df['Transit Time'].iloc[i],
request_id = request_id
) for i in range(df.shape[0])]
for x in new_sailing_entry:
db.session.add(x)
db.session.commit()
return jsonify(api_results) ```
我没看到你在使用 request_id
,但随着更多条目进入 table,这种情况会随着时间的推移而变慢。你可能想要的是
db.session.add(new_user_entry)
db.session.commit()
request_id = new_user_entry.id
因为这将在提交到数据库后具有分配的 ID。但是很难看出你在这种情况下的意图。
我是 Flask 的新手,所以也许我的方法是错误的,但我使用一个路径写入 sqlite 数据库和 return JSON 然后显示在 table .该路由按预期工作,但它在用户端相当慢,因为写入数据库发生在 JSON 被 returned 之前。写入数据库需要 0.1-0.4 秒,所以这不是一个大问题,但如果我能消除这种延迟就更好了。
下面是我的路线。我删除了 new_entry 中的列名,但代码再次按预期运行,只是定时部分比预期的要慢。
注意:我尝试使用 add_all 进行批量插入,但在我测试时它比 for 循环慢。
@app.route('/api/data/<string:from_port>/<string:to_port>/')
def data(from_port,to_port):
# this calls the cargosmart sailing schedule api and converts the json return into the desired format
api_url = helpers.get_api_url(int(un_to_int_dict[from_port]),int(un_to_int_dict[to_port]), api_key=SECRETS.get("big_schedules_api_key"))
df = helpers.create_df(requests.get(url = api_url
, proxies=SECRETS.get("proxies")
),scac_dict = scac_dict)
bigitems = [helpers.create_big_item(df[df['Route ID']==i]) for i in df['Route ID'].unique()]
api_results = {"data":[x.to_dict() for x in bigitems]}
origin_port = from_port
destination_port = to_port
current_date = date.today().strftime("%m/%d/%Y")
new_user_entry = user_data(ip_address = request.remote_addr,
origin_port = origin_port,destination_port = destination_port,date = current_date)
db.session.add(new_user_entry)
request_id = max([x[0] for x in db.session.query(user_data.id).all()])
new_sailing_entry = [cargosmart_sailings(
from_port = df['From Port'].iloc[i],to_port= df['To Port'].iloc[i],terminal = df['To Terminal'].iloc[i],
etd = convert_date(df['ETD'].iloc[i]),
eta = convert_date(df['ETA'].iloc[i]),
vessel_name = df['Vessel Name'].iloc[i],service= df['Service'].iloc[i],
external_voyage_number = df['External Voyage Number'].iloc[i],
direct = df['Direct'].iloc[i],carrier = df['Carrier'].iloc[i],
cutoff = convert_date(df['Cutoff'].iloc[i]),
transit_time = df['Transit Time'].iloc[i],
request_id = request_id
) for i in range(df.shape[0])]
for x in new_sailing_entry:
db.session.add(x)
db.session.commit()
return jsonify(api_results) ```
我没看到你在使用 request_id
,但随着更多条目进入 table,这种情况会随着时间的推移而变慢。你可能想要的是
db.session.add(new_user_entry)
db.session.commit()
request_id = new_user_entry.id
因为这将在提交到数据库后具有分配的 ID。但是很难看出你在这种情况下的意图。