python - Flask 服务器事件在开发中工作但不在生产中
问题描述
我有一个烧瓶路线,应该为浏览器生成服务器事件。基本上该函数的作用是: 1. 加载一个 csv 文件 2. 为 csv 文件的每一行 3. 将用户名和电子邮件保存在 sql 数据库中(使用 sqlalchemy) 4. 更新计数器(用于进度状态) 5. 发送事件到浏览器
问题是,当我处于开发模式(使用烧瓶内置服务器)但在生产模式(使用 NginX 和 gunicorn)下,该功能运行良好,几秒钟后该功能停止,因此计数器永远不会达到 100,它导致浏览器再次调用该函数,并且此循环永远不会结束,因为事件永远不会获得关闭语句。所以主要的问题是,为什么它在开发中而不是在生产中有效?这是mi代码:
# Update or construct database if a csv file was submitted
@app.route("/constructDatabase/<string:filename>", methods=["GET","POST"])
def constructDatabase(filename):
# context manager to open csv file
csvFile = open(os.path.join(os.getcwd(), filename), newline='')
# get lines count of csv file
totalLines = len(csvFile.readlines())
# reset reader pointer
csvFile.seek(0)
# current percent status
current_status = 0
def generate(file, counter):
# unpack and iterate over the csv file to get all the names and emails
for Company,Address,City,State,Zip,County,Phone,Website,Contact,Title,Direct_Phone,Email,Sales,Employees,SIC_Code,Industry in csv.reader(file, delimiter=','):
yield ':keep connection alive\n\n'
counter += 1
# if a user has not contact name or email then not useful
if Email == None or Email == '':
yield f'id: {counter}\nevent: message\ndata: {round(counter / totalLines * 100, 1)}\n\n'
continue
if Contact == None or Contact == '':
yield f'id: {counter}\nevent: message\ndata: {round(counter / totalLines * 100, 1)}\n\n'
continue
# Create user as instance of User class
user = Users(company=Company, address=Address, city=City, state=State,
zip=Zip, country=County, phone=Phone, website=Website, contact=Contact,
title=Title, direct_phone = Direct_Phone, email=Email, sales=Sales,
employees=Employees, sic_code=SIC_Code, industry=Industry)
# Add user to database
db.session.add(user)
# get current percent status of building database
yield f'id: {counter}\nevent: message\ndata: {round(counter / totalLines * 100, 1)}\n\n'
# Save changes in database
db.session.commit()
print("SAVING DATABASE .......")
# close file
file.close()
return Response(generate(csvFile, current_status), mimetype='text/event-stream')
现在的Java脚本代码:
javascript
// create Event source connection with the server to listen for incoming msg
var source = new EventSource(`/constructDatabase/${filename}`);
// if new msg was received
source.onmessage = function(msg) {
// update progress bar
$('.progress-bar').css('width', msg.data+'%').attr('aria-valuenow', msg.data);
// if is 100 percent close connection to the server
if (msg.data == 100) {
source.close();
// Hide label
$('.prog-bar-label').addClass('d-none');
// Hide CSV progress bar
$('.csvProgressBar').addClass('d-none');
// reset progress bar
$('.csvProgressBar').find('.progress-bar').css('width', 0+'%').attr('aria-valuenow', 0);
}
};
source.onerror = function(error){
console.log(error.data);
};
解决方案
我弄清楚这里发生了什么。问题是烧瓶开发服务器,当它运行时,他没有声明任何超时,所以一个函数可能会等待很长时间的响应。但是在使用gunicorn的生产服务器中,它的默认超时时间很短,所以如果一个函数需要很长时间才能得到响应并且达到超时时间,gunicorn会杀死进程并退出函数,所以浏览器端的流式处理,认为那是服务器中的错误,当时只是一个延迟的功能。解决方案是使用 --timeout (此处为所需的超时值)增加 gunicorn 中的超时,并且会很好。不要使用非常大的超时值,只需尝试弄清楚您的函数需要多长时间才能获得响应并使用该值。请记住,此值现在对于您的所有函数都是相同的。
推荐阅读
- django - 来自 Postman 的 Django DRF 和 XLSX 二进制文件上传
- python - 根据元组的值以不同的顺序对元组列表进行排序
- python - 为什么在组合两个数据帧时会出现“可散列”错误?
- javascript - `screen` 带来了什么价值?
- amazon-web-services - AWS DMS 错误 SQL_ERROR SqlState:28000
- php - Wordpress 网站使用删除滑块上的按钮(独白)
- android - 如何在列表视图顶部添加一行
- ios - 在 Swift 中使用 JSONDecoder 在解析 JSON 时将空值转换为默认字符串
- excel - INDIRECT 不与 ADDRESS 一起工作以形成范围
- c# - 在存储库服务中检索用户 http 数据