1
John60676 2018-04-15 23:45:51 +08:00
上代码
|
2
charlescwj OP @John60676 这是我 views 代码的一部分 觉得问题应该就在这里
from app import app from flask import render_template,request,flash,session from .models import CostForm,dowmlist import numpy as np from flask import make_response # 全局基础数据 Salary_Increase = 0.02 #工资涨幅, Rate_Insurance = 0.48 #五险一金占年薪的百分比 Rate_Welfare = 0.06 #其他福利占年薪的百分比 Rate_CommonRepair = 0.55 Rate_BigRepair = 0.45 Rate_LineRepair = 0.5 Rate_StructureRepair = 0.2 Rate_DeviceRepair = 0.3 TrainPower = 1150 #列车额定功率,1150kw Rate_power = 0.8 #功率利用率 Rate_water = 0.07 Rate_gas = 0.05 Rate_heat = 0.06 Rate_transport = 0.14 Rate_accident = 0.34 Rate_ticket = 0.25 Rate_clean = 0.11 Rate_ManagerSalary = 0.35 Rate_Active = 0.25 Rate_Travel = 0.3 Rate_Education = 0.1 # 存放提交的基础数据 datum={} # 存放计算后的数据 LineLength = [] Flow = [] TrainCount = [] StaffCount = [] TrainRunKM = [] Salary = [] Total_Salary = [] TrainRepair = [] OtherRepair = [] Train_electric = [] Lighting_electric = [] Total_electric = [] Operation = [] Management = [] TotalCost = [] @app.route('/') def index(): k = request.remote_addr m = request.user_agent session['user'] = k+m.browser+m.platform+m.string+m.version return render_template('Default.html') @app.route('/base') def base(): return render_template('base.html') @app.route('/Input', methods=('GET', 'POST')) def register(): form = CostForm() if form.validate_on_submit(): flash("sucess") # 其他数据 datum['Cal_year']=form.text_year.data datum['Investment']=form.text_investment.data datum['speed']=form.text_speed.data datum['BaseSalary']=form.text_salary.data datum['Price_TrainRepair']=form.text_trainRepair.data datum['Price_OtherRepair']=form.text_repair.data datum['Price_electric']=form.text_e_cost.data datum['Price_Operation']=form.text_operation.data datum['Rate_management']=form.text_management.data/100 for i in range(form.text_year.data): if i<3: LineLength.append(form.text_length1.data) Flow.append((0.8 + i / 10)*form.text_flow1.data) TrainCount.append((0.8 + i / 10) * form.text_train1.data) StaffCount.append((0.8 + i / 10) * form.text_person1.data) Lighting_electric.append(datum['Price_electric'] * (0.8 + i / 10) * form.text_e1.data) elif 3<=i<10: LineLength.append(form.text_length1.data) Flow.append(form.text_flow1.data+(i - 2) * (form.text_flow2.data - form.text_flow1.data)/ 7) TrainCount.append(form.text_train1.data+(i - 2) * (form.text_train2.data - form.text_train1.data)/ 7) StaffCount.append(form.text_person1.data+(i - 2) * (form.text_person2.data - form.text_person1.data)/ 7) Lighting_electric.append(datum['Price_electric'] *( form.text_e1.data + (i - 2) * (form.text_e2.data - form.text_e1.data) / 7)) elif 10<=i<25: LineLength.append(form.text_length2.data) Flow.append(form.text_flow2.data + (i - 9) * (form.text_flow3.data - form.text_flow2.data) /15) TrainCount.append(form.text_train2.data + (i - 9) * (form.text_train3.data - form.text_train2.data)/15) StaffCount.append(form.text_person2.data + (i - 9) * (form.text_person3.data - form.text_person2.data)/15) Lighting_electric.append(datum['Price_electric']*(form.text_e2.data + (i - 9) * (form.text_e3.data - form.text_e2.data)/15)) else: LineLength.append(form.text_length3.data) Flow.append(form.text_flow3.data) TrainCount.append(form.text_train3.data) StaffCount.append(form.text_person3.data) Lighting_electric.append(datum['Price_electric'] * form.text_e3.data) TrainRunKM.append(LineLength[i] * TrainCount[i] * 2 * 365 * 1.1 / 10000) Salary.append(LineLength[i] * StaffCount[i] * datum['BaseSalary'] * (1 + Salary_Increase)**i) Total_Salary.append(round(Salary[i] * (1 + Rate_Insurance + Rate_Welfare),1)) TrainRepair.append(round(datum['Price_TrainRepair'] * TrainRunKM[i],1)) OtherRepair.append(round(datum['Price_OtherRepair'] * TrainRunKM[i],1)) # Train_electric[i] = datum['Price_electric'] * TrainPower * Rate_power * TrainRunKM[i] / datum['speed'] Train_electric.append(round(datum['Price_electric']* 5 * TrainRunKM[i],1)) Total_electric.append(round(Train_electric[i] + Lighting_electric[i],1)) Operation.append(round(datum['Price_Operation'] * TrainRunKM[i],1)) TotalCost.append(round((Total_Salary[i] + TrainRepair[i] + OtherRepair[i] + Total_electric[i] + Operation[ i]) / (1-datum['Rate_management']),1)) Management.append(round(datum['Rate_management'] * TotalCost[i],1)) # 添加跳转页面或者弹窗 return render_template('Input.html', form=form) @app.route('/simple') def simple(): year=datum['Cal_year'] all_total_salary = round(sum(Total_Salary),1) all_trainrepair = round(sum(TrainRepair),1) all_otherrepair = round(sum(OtherRepair),1) all_total_electric = round(sum(Total_electric),1) all_operation = round(sum(Operation),1) all_management = round(sum(Management),1) all_totalcost = round(sum(TotalCost),1) return render_template('simple.html', all_total_salary=all_total_salary,all_trainrepair=all_trainrepair,all_otherrepair=all_otherrepair,all_total_electric=all_total_electric,all_operation=all_operation,all_management=all_management,all_totalcost=all_totalcost,year=year,Total_Salary=Total_Salary,TrainRepair=TrainRepair,OtherRepair=OtherRepair,Total_electric=Total_electric,Operation=Operation,TotalCost=TotalCost,Management=Management) |
3
charlescwj OP |
4
John60676 2018-04-16 00:10:53 +08:00
你别把表单数据放在文件中
|
5
John60676 2018-04-16 00:16:42 +08:00
# 存放提交的基础数据
datum={} # 存放计算后的数据 . . . TotalCost = [] 这段代码的问题,用户提交的数据都写在了全局变量中,所以会出现你所说的有时候显示 20,有时候显示 30。这是因为用户 A 提交表单后,计算后的数据放在了全局变量中,此时,所有用户访问看到的都是用户 A 的结果。用户 B 提交表单时同理 |
6
charlescwj OP @John60676 奥 原来如此 感谢感谢 然后 我还想问一下 要是不使用全局变量 怎么在不同的请求直接传递数据? 存到数据库或者保存成 json 文件?
|
7
charlescwj OP @John60676 这么晚了 还帮我解答问题 再来感谢一下 👍👍
|
8
John60676 2018-04-16 00:33:36 +08:00
看需求,全局变量也不是不可以用,把数据写成 dict 就好了,用以用户名或者其他标识用户的字符串作为 key 值。这样就不会复写了。不过最简单的还是用数据库吧
|
9
charlescwj OP @John60676 👏 感谢 我明天改一下 晚安😊
|
10
kaneg 2018-04-16 09:09:32 +08:00 via iPhone
不能滥用全局变量,这在大多数编程语言都是大忌
|
11
charlescwj OP @John60676 您好 我还想问一下 按照我的理解 各个进程之间是相互独立的,互不影响的 为什么这里会出现这样的问题呢? http://www.ziyoubaba.com/archives/585 这个人的说法让我有些迷糊了 请帮我解答一下(新手 ,在学习中~ 勿怪)
|
12
jimzhong 2018-04-16 09:41:06 +08:00
我猜 gunicorn 每个 worker 的全局变量是共享的。
|
13
John60676 2018-04-16 14:42:46 +08:00
@charlescwj @jimzhong 每个 worker 的全局变量是独立的。但是我们回到这个问题上看。楼主开了三个 worker,这就导致了每次访问的时候分配的 worker 不一定是上一次分配到的那个 worker。所以会出现这样的问题。你可以试试把 worker 改成一个,再试试,就能看到每次的结果都一样了
|