Python社区  »  DATABASE

Flask没有从新的sql数据更新数据-Nginx,Uwsgi,Centos7,Mysql

Andrew Venson • 1 周前 • 28 次点击  

这是我的代码,我将显示uwsgi.py、main.py的一些部分(特别是在添加了2个查询的地方)以及nginx.conf。。。干得好。。。 这只是该项目的一个基本内容。我真的不认为这在我的代码买我可能是错的。 主.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))

wsgi.py公司

from main import app as application

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

Itinappinch_rep.ini公司

[uwsgi]
module = wsgi


master = true
processes = 5


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


die-on-term = true

[Unit]
Description=uWSGI instance to serve itinapinch_rep
After=network.target


[Service]
User=reports
Group=nginx
WorkingDirectory=/home/reports/itinapinch_rep
Environment="PATH=/home/reports/itinapinch_rep/it_venv/bin"
ExecStart=/home/reports/itinapinch_rep/it_venv/bin/uwsgi --ini itinapinch_rep.ini

[Install]
WantedBy=multi-user.target
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/56747
 
28 次点击  
分享到微博
文章 [ 1 ]  |  最新文章 1 周前
snakecharmerb
Reply   •   1 楼
snakecharmerb    10 月前

根据 docs sqlalchemy.engine.Connection :

Connection对象表示从连接池签出的单个dbapi连接。在此状态下,连接池对连接没有影响,包括其过期或超时状态。要使连接池正确管理连接,只要连接未在使用中,就应将连接返回到连接池(即connection.close())。

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