Flask不从新的SQL数据更新数据-Nginx,Uwsgi,Centos7,Mysql

时间:2019-05-24 22:29:32

标签: python mysql nginx flask wsgi

我有一个用烧瓶创建的Web报告工具,该工具连接到mysql数据库,该数据库全天从电话系统接收数据。我有这个报告工具,可以很好地工作(在刷新屏幕或登录时更新数字。)但是现在,自从我添加了2个查询并稍微更改了代码后,我仍然可以访问该网站,但是不会像以前那样更新其数字。重启我的uwsgi服务和nginx时,这些数字是正确的,但是随着时间的流逝,这些数字不会更新,并且直到服务和nginx重新启动时才打来电话。在部署和服务器维护方面,我是个菜鸟。我不确定是否需要在代码中的某个地方关闭sql连接并再次建立连接,以便我们收到更新的数字? 这是我的代码,我将显示uwsgi.py,main.py的某些部分(尤其是在其中添加了2个查询的位置)以及nginx.conf…在这里,您可以... 这只是项目的基本内容。我真的不认为这在我的代码购买中可能是错误的。 Main.py

ButtonProps

wsgi.py

app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret'
app.config['SQLALCHEMY_DATABASE_URI'] = "mysql+pymysql://root@localhost:3306/asteriskcdrdb"
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
db = SQLAlchemy(app)
bcrypt = Bcrypt(app)
login_manager = LoginManager()
login_manager.init_app(app)
engine = db.engine
connection = engine.connect()

location_to_phone = {
    "TX-Billing": "5123597546",
    "TX-Bee Cave": "5123668568",
    "TX-Central Austin": "5124543781",
    "TX-North Austin": "5128373376",
    "TX-Pflugerville": "5122523700",
    "TX-San Antonio": "2106160448",
    "TX-Steiner Ranch": "5122660007",
    "LA-Baton Rouge": "2553039500",
    "LA-Bossier City": "3187425124",
    "La-Lafayette": "3378392773",
    "La-Old Metairie": "5048362050",
    "La-Shreveport": "3186862021",
    "LA-Uptown": "5048975899"
}

location_to_center = {
    "TX-Billing": {"Front Desk": "7000", "Medical": "7001"},
    "TX-Bee Cave": {"Front Desk": "7040", "Medical": "7041"},
    "TX-Central Austin": {"Front Desk": "7050", "Medical": "7051"},
    "TX-North Austin": {"Front Desk": "6000", "Medical": "7031"},
    "Tx-Pflugerville": {"Front Desk": "7070", "Medical": "7071"},
    "Tx-San Antonio": {"Front Desk": "7060", "Medical": "7061"},
    "Tx-Steiner Ranch": {"Front Desk": "7120", "Medical": "7121"},
    "LA-Baton Rouge": {"Front Desk": "7080", "Medical": "7081"},
    "LA-Bossier City": {"Front Desk": "0", "Medical": "0"},
    "La-Lafayette": {"Front Desk": "7100", "Medical": "7101"},
    "La-Old Metairie": {"Front Desk": "0", "Medical": "0"},
    "La-Shreveport": {"Front Desk": "0", "Medical": "0"},
    "LA-Uptown": {"Front Desk": "0", "Medical": "0"}
}

def reports():
    if current_user.is_authenticated:
        print("Authenticated")
    else:
        return redirect(url_for('login'))

    form = ReportConfig(prefix='a')
    form2 = Details(prefix='b')
    form3 = ReportConfig2(prefix='c')

    start_date = datetime.today().strftime('%Y-%m-%d')
    end_date = datetime.now().strftime("%Y-%m-%d")

    totals = []
    answered = []
    no_answer = []
    average = []
    client_num = []
    medicals = []
    fronts = []

    calls = 0
    notan = 0
    ans = 0


    for (loc, num), (site, data) in zip(location_to_phone.items(),location_to_center.items()):
        md = data['Medical']
        fd = data['Front Desk']
        test = num


        totalcalls = connection.execute(f"SELECT COUNT(calldate) FROM cdr WHERE did='{test}' AND NOT lastapp='background' and calldate between '{start_date} 08:00:00' and '{end_date} 23:59:59'")
        answered_count = connection.execute(f"SELECT COUNT(calldate) FROM cdr WHERE did='{test}' AND NOT lastapp='background' AND disposition='ANSWERED' and calldate between '{start_date} 08:00:00' and '{end_date} 23:59:59'")
        no = connection.execute(f"SELECT COUNT(calldate) FROM cdr WHERE did='{test}' AND NOT lastapp='background' AND disposition='NO ANSWER' and calldate between '{start_date} 08:00:00' and '{end_date} 23:59:59'")
        av = connection.execute(f"SELECT duration from cdr WHERE did='{test}' AND NOT lastapp='background' AND calldate between '{start_date} 08:00:00' AND '{end_date} 23:59:59'")
        medical = connection.execute(f"SELECT COUNT(calldate) FROM cdr WHERE did='{test}' AND NOT lastapp='background' and dst='{md}' and calldate between '{start_date} 08:00:00' and '{end_date} 23:59:59'")
        front_desk = connection.execute(f"SELECT COUNT(calldate) FROM cdr WHERE did='{test}' AND NOT lastapp='background' and dst='{fd}' and calldate between '{start_date} 08:00:00' and '{end_date} 23:59:59'")
        answer_num = [row[0] for row in answered_count]

        nono = [row[0] for row in no]

        total = [row[0] for row in totalcalls]

        med = [row[0] for row in medical]
        front = [row[0] for row in front_desk]


        sum = 0
        count = total[0]

        calls = calls + total[0]
        notan = notan + nono[0]
        ans = ans + answer_num[0]
        for x in av:
            sum = sum + x[0]

        try:
            avg = (sum/count)
        except:
            avg = 0
        # avg = (sum/count)
        average.append(round(avg, 2))
        totals.append(total[0])
        answered.append(answer_num[0])
        no_answer.append(nono[0])
        client_num.append(test)
        medicals.append(med[0])
        fronts.append(front[0])

return render_template('reports.html', start_date = start_date, month_date = start_date, assign=assign, form=form, form2=form2,  form3=form3, end_date = end_date, all_calls=calls, all_answered=ans, not_answered=notan, location=zip(totals, location_to_phone, answered, no_answer, average, client_num, medicals, fronts))

itinapinch_rep.ini

from main import app as application

if __name__ == '__main__':
    application.run()

itinapinch_rep.service

[uwsgi]
module = wsgi


master = true
processes = 5


socket = itinapinch_rep.sock
chmod-socket = 660
vacuum = true


die-on-term = true

1 个答案:

答案 0 :(得分:1)

根据sqlalchemy.engine.Connection的{​​{3}}:

  

Connection对象表示从连接池中检出的单个dbapi连接。在这种状态下,连接池不会影响连接,包括连接的到期或超时状态。为了使连接池正确管理连接,每当不使用连接时,应将连接返回到连接池(即connection.close())。

因此,不要在导入时将其创建为全局对象,而应在每个请求的生命周期内创建和关闭连接。