gpt4 book ai didi

Linux Azure Web App initial request has very slow response time(Linux Azure Web App初始请求的响应时间非常慢)

转载 作者:bug小助手 更新时间:2023-10-22 17:34:48 25 4
gpt4 key购买 nike



I don’t have any experience with web deployment so there will definitely be gaps in my knowledge, so I do apologise. I am creating a Flask web app that uses the OpenAI API that basically allows users to converse with the bot and save the conversation into the database.
I am puzzled why doing a POST request to the web server can result in the server taking 2-5 minutes to respond. The most confusing part of this is that only the INITIAL request is dreadfully slow. After the request is returned, the web application functions as normal, having acceptable response timings.

我没有任何网络部署的经验,所以我的知识肯定会有差距,所以我道歉。我正在创建一个Flask网络应用程序,该应用程序使用OpenAI API,基本上允许用户与机器人对话并将对话保存到数据库中。我很困惑为什么对web服务器进行POST请求会导致服务器需要2-5分钟才能做出响应。其中最令人困惑的部分是,只有INITIAL请求的速度慢得可怕。在返回请求之后,web应用程序正常工作,具有可接受的响应定时。


Testing it locally on the machine no issues are present, yet deployed in production it displays very slow response times with the initial request. I don’t believe it is the API at fault, after testing. I think the issue either lies in the library I am using ‘flask-turbo’ (might behave differently when deployed) or how I have configured the Linux web application.

在机器上进行本地测试,没有出现任何问题,但在生产中部署时,它对初始请求的响应速度非常慢。经过测试,我不认为API有错。我认为问题要么在于我使用的“flask turbo”库(部署时可能会有不同的行为),要么在于我如何配置Linux web应用程序。


I have configured the web app to ‘Always On’ and tried tweaking the HTTP version and ARR affinity with no success. I have Scaled Up (Basic B1) and Scaled Out (3 instances) the application with no success as well. Any help or advice would be much appreciated.

我已经将web应用程序配置为“始终打开”,并尝试调整HTTP版本和ARR亲和力,但没有成功。我对应用程序进行了扩展(基本B1)和扩展(3个实例),但也没有成功。如有任何帮助或建议,我们将不胜感激。


app.py code.

app.py代码。


conversation_history = []
@app.route("/", methods=["POST", "GET"]) # this sets the route to this page
def chat_page():
if request.method == "POST":
if 'submit-input-button' in request.form:
input_text = request.form.get("input-prompt")
if turbo.can_stream() and input_text.strip():
# Tracking chat conversation and posting to API
chat_response = get_chatgpt_response(conversation_history)
conversation_history.append(chat_response)

return turbo.stream([turbo.append("> " + input_text + "\n" + chat_response + "\n\n", 'output-text'), turbo.update('', 'input_error_text'), turbo.update('', 'database_error_text')])
else:
return turbo.stream([turbo.update('Please enter an input', 'input_error_text'), turbo.update('', 'database_error_text')])
elif 'submit-answers-button' in request.form:
# Submitting text from the output window to the database
output_text = request.form.get("output-text")
title_num = request.form.get("scenario-num")

if post_to_database(title_num, output_text):
return turbo.stream([turbo.update('Successfully posted to database!', 'database_error_text')])
else:
return turbo.stream([turbo.update('Error: Unable to post to the database', 'database_error_text')])

return render_template("index.html", input_error_text="", database_error_text="", scenario_num=key, scenario_title=scenario_obj["title"], scenario_desc=scenario_obj["desc"])


if __name__ == "__main__":
app.run()

index.html code

index.html代码


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="{{ url_for('static', filename='styles.css') }}" />
<!-- <link rel="stylesheet" href="../static/styles.css" /> -->
{{ turbo() }}
<title>ChatGPT Web App</title>
</head>
<body>
<main>
<div id="main-heading" class="main-container match-bg-color">
<h1>ChatGPT Test</h1>
</div>
<div id="scenario-section" class="main-container match-bg-color">
<h2 id="scenario-heading">Scenario {{ scenario_num }} - {{ scenario_title }}</h2>
<p id="scenario-description">{{ scenario_desc }}</p>
</div>
<form method="post" id="chat-bot-form">
<!-- Input makes it possible to retrieve the scenario number -->
<input type="text" name="scenario-num" value="{{ scenario_num }}" hidden />
<div id="output-container" class="main-container">
<label for="output-text" class="main-label">Output</label>
<textarea
name="output-text"
id="output-text"
class="text-box"
rows="18"
readonly
>
{{ output_text }}</textarea
>
</div>
<div id="input-container" class="main-container">
<label for="input-prompt" class="main-label">Input</label>
<textarea
name="input-prompt"
id="input-prompt"
class="text-box"
rows="5"
placeholder="Enter your prompt here..."
></textarea>
<div class="input-button-container">
<button type="submit" id="submit-input" name="submit-input-button"
>Enter</button
>
<p class="bold-text warning-text" id="input_error_text">{{custom_text}}</p>
</div>
</div>
<div class="main-container match-bg-color" id="submit-answers-container">
<button id="submit-answers-button" name="submit-answers-button"
>Submit Your Answers</button
>
<p class="bold-text warning-text" id="database_error_text">{{custom_text}}</p>
</div>
</form>
</main>
<script>
// Automatically scrolls text down
let textarea = document.getElementById("output-text");
textarea.addEventListener("selectionchange", function () {
console.log("Scroll down man");
textarea.scrollTop = textarea.scrollHeight;
});
</script>
</body>
</html>


Edit: Added code. However app.py needed to have some code removed and modified (such as API call) due to maintaining academic integrity. index.html is unmodified. I should also point out this also my first time using Flask.

编辑:添加了代码。然而,由于保持学术完整性,app.py需要删除和修改一些代码(例如API调用)。index.html未修改。我还应该指出这也是我第一次使用烧瓶。


更多回答

Please share the code that which you tried.

请分享您尝试的代码。

I have added code to my question. Hopefully it helps.

我已经为我的问题添加了代码。希望它能有所帮助。

优秀答案推荐


  • Consider using a production-ready WSGI server like Gunicorn or uWSGI instead of the built-in Flask development server. These servers are designed for production use and often offer better performance and stability.


gunicorn_config.py:

gunicorn_config.py:


# gunicorn_config.py

import multiprocessing

# Bind to the IP and port where your application should listen.
bind = "0.0.0.0:8000" # Replace with your desired IP and port.

# Number of Gunicorn worker processes to spawn.
workers = multiprocessing.cpu_count() * 2 + 1 # Adjust as needed based on your server's resources.

# Set the worker class to use. 'gevent' and 'eventlet' are alternative worker classes
# that can be more efficient for I/O-bound applications. The default is 'sync'.
worker_class = "sync"

# Number of worker threads per worker process. Adjust as needed.
threads = 2

# Specify your Flask application's entry point (usually the app variable in your app.py).
# Replace 'app_name' with your actual application's name.
# You can also specify the Python module that contains your app using the syntax 'module:app'.
app = "app_name:app"

# Enable or disable daemon mode. Daemon mode runs Gunicorn in the background.
daemon = False

# Set the location of Gunicorn's error log.
errorlog = "/var/log/gunicorn/error.log" # Adjust the path as needed.

# Set the location of Gunicorn's access log.
accesslog = "/var/log/gunicorn/access.log" # Adjust the path as needed.

# Enable access log format. Common options are "combined", "common", "short", and "tiny".
# You can also specify your custom log format.
access_log_format = "%(h)s %(l)s %(u)s %(t)s \"%(r)s\" %(s)s %(b)s \"%(f)s\" \"%(a)s\""

# Configure the maximum number of requests a worker will process before restarting.
max_requests = 1000

# Configure the maximum number of requests a worker will process before graceful restart.
max_requests_jitter = 50

# Set the timeout for requests (in seconds). Adjust as needed.
timeout = 60

# Enable keep-alive connections.
keepalive = 2

# Set the maximum number of headers that the server will accept.
max_request_header_size = 8192

# Set the maximum size of the request body.
max_request_body_size = 1048576 # 1 MB

# Preload the application before the worker processes are forked.
preload_app = True

# Disable worker process auto-reloading.
reload = False

Please check how to update gunicorn HTTP Headers on Azure App Service for reference.

请检查如何在Azure应用程序服务上更新gunicorn HTTP标头以供参考。


Response time:

响应时间:


enter image description here


更多回答

I've followed your answer and when deploying to the server I'm getting ModuleNotFoundError: No module named '–bind=0'. I have added gunicorn as a module in the requirements.txt so I'm not sure why this is happening.

我已经按照你的回答进行了部署,当部署到服务器时,我得到ModuleNotFoundError:没有名为“–bind=0”的模块。我在requirements.txt中添加了gunicorn作为模块,所以我不确定为什么会发生这种情况。

@Programmer367 You are trying to run Gunicorn with the -bind=0 argument, which is not a valid argument for Gunicorn. The bind configuration should be specified in the Gunicorn configuration file (gunicorn_config.py in the example provided) rather than as a command-line argument.

@Programmaber367您正试图使用-bind=0参数运行Gunicorn,这对Gunicorn来说不是有效的参数。绑定配置应该在Gunicorn配置文件中指定(在提供的示例中为Gunicorn_config.py),而不是作为命令行参数。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com