首页 > 解决方案 > 带有 Pandas 的 Flask App 在开发模式或 gunicorn 中的工作方式不同

问题描述

我必须比较两个温度数据框并使用烧瓶将其提供给用户。我的数据在 MariaDb 数据库中。

所以我收到的请求 Get 是要比较的两个温度, start_date , end_date 和比较值

当我在烧瓶中收到 GET 请求时,我为第一个温度创建第一个数据帧,并为第二个温度创建第二个数据帧

import flask
import pandas as pd
from sqlalchemy import create_engine, text

# engine creation 
engine = create_engine("mysql+pymysql://user:pass@localhost/db",    echo=True)
conn_sql_alchemy = engine.connect()

# function to create dataframe for
def create_dataframe(obj, start_date, end_date):
    # with obj, start_date and end_date I create a request like this one
    sql_sp_query = text("select  timestamp, value from db.tldata where pointId = (select Id from db.device_pointList where DeviceId = ( select Id from db.device where device_instance =200) and  ObjectId = 9 and object='trendLog') and timestamp between '2019-10-30 00:00:00' and '2019-10-31 23:59:59'")
    #creation of dataframe
    pd_datas = pd.read_sql_query(sql_sp_query, conn_sql_alchemy, index_col="timestamp")
    # return dataframe
    return pd_datas

# flask route
@app.route('/analyse_co/<object_sp>/<object_ai>/<start_date>/<end_date>/<difference>')
def analyse_co(object_sp, object_ai, start_date, end_date, difference):
    print('creation of dataframe for object_ai')
    pd_ai_datas = create_dataframe(object_ai, start_date, end_date)
    print(pd_ai_datas)
    print('creation of dataframe for object_sp')
    pd_sp_datas = create_dataframe(object_sp, start_date, end_date)
    print(pd_sp_datas)
    return 'finish'

当我在开发模式下尝试我的应用程序时,我得到的结果是我正在等待我在控制台上打印我的两个数据框并在网页上看到完成

控制台上的结果: creation of dataframe for object_ai 2019-11-08 08:34:32,113 INFO sqlalchemy.engine.base.Engine select timestamp, value from db.tldata where pointId = (select Id from db.device_pointList where DeviceId = ( select Id from db.device where device_instance =200) and ObjectId = 7 and object='trendLog') and timestamp between '2019-10-30 00:00:00' and '2019-10-31 23:59:59' 2019-11-08 08:34:32,114 INFO sqlalchemy.engine.base.Engine {} timestamp value 2019-10-30 00:00:00 10.475613 2019-10-30 00:15:00 8.461939 2019-10-30 00:30:00 5.507755 2019-10-30 00:45:00 8.461939 2019-10-30 01:00:00 5.507755 ... ... 2019-10-31 22:45:00 10.641429 2019-10-31 23:00:00 1.556020 2019-10-31 23:15:00 11.016837 2019-10-31 23:30:00 8.576020 2019-10-31 23:45:00 9.379898 [192 rows x 1 columns] creation of dataframe for object_sp 2019-11-08 08:34:32,222 INFO sqlalchemy.engine.base.Engine select timestamp, value from db.tldata where pointId = (select Id from db.device_pointList where DeviceId = ( select Id from db.device where device_instance =200) and ObjectId = 9 and object='trendLog') and timestamp between '2019-10-30 00:00:00' and '2019-10-31 23:59:59' 2019-11-08 08:34:32,223 INFO sqlalchemy.engine.base.Engine {} timestamp value 2019-10-30 00:00:00 14.265137 2019-10-30 00:15:00 15.733887 2019-10-30 00:30:00 17.308594 2019-10-30 00:45:00 18.942383 2019-10-30 01:00:00 20.517090 ... ... 2019-10-31 22:45:00 03.228516 2019-10-31 23:00:00 04.716309 2019-10-31 23:15:00 06.372070 2019-10-31 23:30:00 07.834961 2019-10-31 23:45:00 09.363281 [192 rows x 1 columns] 10.23.1.41 - - [08/Nov/2019 08:34:32] "GET /analyse_co/200.tl9/200.tl7/2019-10-30/2019-10-31/2 HTTP/1.1" 200 -

当我在 HTTPS 中使用 nginx 和 gunicorn 尝试我的应用程序时,在日志上我只看到我的第一个数据帧的创建,第二个被忽略,我的网页上没有错误,我看到完成......

Nov 8 08:35:04 rockpro64 gunicorn[24608]: creation of dataframe for object_ai Nov 8 08:35:04 rockpro64 gunicorn[24608]: 2019-11-08 08:35:04,050 INFO sqlalchemy.engine.base.Engine select timestamp, value from db.tldata where pointId = (select Id from db.device_pointList where DeviceId = ( select Id from db.device where device_instance =200) and ObjectId = 7 and object='trendLog') and timestamp between '2019-10-30 00:00:00' and '2019-10-31 23:59:59' Nov 8 08:35:04 rockpro64 gunicorn[24608]: 2019-11-08 08:35:04,050 INFO sqlalchemy.engine.base.Engine {} Nov 8 08:35:04 rockpro64 gunicorn[24608]: value Nov 8 08:35:04 rockpro64 gunicorn[24608]: timestamp Nov 8 08:35:04 rockpro64 gunicorn[24608]: 2019-10-30 00:00:00 10.475613 Nov 8 08:35:04 rockpro64 gunicorn[24608]: 2019-10-30 00:15:00 8.461939 Nov 8 08:35:04 rockpro64 gunicorn[24608]: 2019-10-30 00:30:00 5.507755 Nov 8 08:35:04 rockpro64 gunicorn[24608]: 2019-10-30 00:45:00 8.461939 Nov 8 08:35:04 rockpro64 gunicorn[24608]: 2019-10-30 01:00:00 5.507755 Nov 8 08:35:04 rockpro64 gunicorn[24608]: ... ... Nov 8 08:35:04 rockpro64 gunicorn[24608]: 2019-10-31 22:45:00 10.641429 Nov 8 08:35:04 rockpro64 gunicorn[24608]: 2019-10-31 23:00:00 1.556020 Nov 8 08:35:04 rockpro64 gunicorn[24608]: 2019-10-31 23:15:00 11.016837 Nov 8 08:35:04 rockpro64 gunicorn[24608]: 2019-10-31 23:30:00 8.576020 Nov 8 08:35:04 rockpro64 gunicorn[24608]: 2019-10-31 23:45:00 9.379898 Nov 8 08:35:04 rockpro64 gunicorn[24608]: [192 rows x 1 columns] Nov 8 08:35:04 rockpro64 gunicorn[24608]: creation of dataframe for object_sp Nov 8 08:35:04 rockpro64 gunicorn[24608]: 2019-11-08 08:35:04,187 INFO sqlalchemy.engine.base.Engine select timestamp, value from db.tldata where pointId = (select Id from db.device_pointList where DeviceId = ( select Id from db.device where device_instance =200) and ObjectId = 9 and object='trendLog') and timestamp between '2019-10-30 00:00:00' and '2019-10-31 23:59:59' Nov 8 08:35:04 rockpro64 gunicorn[24608]: 2019-11-08 08:35:04,187 INFO sqlalchemy.engine.base.Engine {}

标签: python-3.xpandasflaskgunicorn

解决方案


推荐阅读