我有一个 React-Flask 应用程序,现在我已经在 Docker 容器中运行以进行部署。该应用程序通过 FormData 将大文件发送到 Flask 服务器(总共近 200 MB)。要部署应用程序,我使用带有以下命令的gunicorn:
gunicorn --workers 6 --timeout 500 --graceful-timeout 500 --bind 0.0.0.0:5001 wsgi:app
在本地运行时(无论是在 Flask 开发模式还是在本地运行 Gunicorn),大约需要 3 秒才能将请求返回给客户端,但在 docker 上部署时需要超过 30 秒。
我的操作系统是 macOS M1。 Docker 正在使用 alpine 运行。
关于为什么会造成这种情况有什么想法吗?或者有什么解决办法吗?
我尝试在 Flask 中将文件解析作为异步任务运行,但我不断从中收到其他错误消息。下面的代码显示了在开发模式下“快”但在 Docker 中慢的工作版本。
/**
* Sends data to the server using a POST request.
* @function sendDataAsync
* @returns {void}
*/
function sendDataAsync() {
const formData = new FormData();
genomes.forEach((genome, i) => {
const { genomefasta, genomeannotation } = genome[`genome${i + 1}`];
formData.append("genomefasta", genomefasta);
genomeannotation.forEach((annotation, k) => {
formData.append(`genomeannotation${i + 1}`, annotation);
});
const rep = replicates[i][`genome${i + 1}`];
rep.forEach((replicate, j) => {
const letter = String.fromCharCode(97 + j);
const { enrichedforward, enrichedreverse, normalforward, normalreverse } = replicate[`replicate${letter}`];
// gather the data into the formData object
});
});
formData.append("alignmentfile", alignmentFile);
fetch("/api/runAsync/", {
method: "POST",
body: formData,
})
.then((response) => response.json())
.then((data) => {
setLoading([false, false]);
if (data.result === "success") {
// open result in new tab
let filePath = data.id;
let URL = `/status/${filePath}`
window.open(URL, "_blank", "noopener,noreferrer");
} else {
console.log(data);
}
})
.catch((err) => console.log(err));
}
@app.route('/api/runAsync/', methods=['POST', 'GET'])
def getInputAsync():
'''get the input from the form and execute TSS prediction'''
# save start time
startTime = time()
request_files = request.files.to_dict(flat=False) # Only this line takes around 30 sec.
result = do_some_process(files)
# print runnning time
print("Time: " + str(time() - startTime))
return jsonify({'result': 'success', "id": result['id']})
当您部署在 docker 容器上时,您的服务器将获得一个
fixed number vcpu
来降低本地机器的性能。当您使用许多工作人员时,由于工作人员会争夺资源,因此会导致性能下降。例如,您的容器提供 1 个 vcpu,但您的 Gunicorn 运行 6 个工作线程。它会比你在本地运行可以使用完整资源的gunicorn慢。
--workers 1
并增加选择最合适的配置