我正在使用女服务员在本地主机(Windows VM)上提供 API,它由在同一 Windows VM 上运行的 C# 应用程序调用。当女服务员挂起时,C# 报告超时错误:
System.Net.WebException: The operation has timed out at System.Net.HttpWebRequest.GetResponse()
我可以通过转到启动女服务员的命令提示符并按 CTRL+C 来让女服务员恢复响应。当我这样做时,我从服务员那里收到了几条消息:WARNING:waitress.queue:Task queue depth is 6
,但它继续工作。
女服务员是这样配置的:
from flask import Flask, request
from waitress import serve
app = Flask(__name__)
@app.route('/apiname', methods=['POST'])
def apiname():
content = request.json
foo = function(content)
return foo
if __name__ == '__main__':
serve(app, listen='*:5000', threads=1)
我和女服务员一起跑
> python appname.py
如果重要的话,C# 代码存根是:
string API_URL = "api_url";//insert a real URL here
var httpWebRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(API_URL);
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
httpWebRequest.Timeout = 100000; // milliseconds
using (var streamWriter = new System.IO.StreamWriter(httpWebRequest.GetRequestStream()))
{
string json = Newtonsoft.Json.JsonConvert.SerializeObject(info, new Newtonsoft.Json.JsonSerializerSettings { ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore });
streamWriter.Write(json);
streamWriter.Flush();
streamWriter.Close();
}
var httpResponse = (System.Net.HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new System.IO.StreamReader(httpResponse.GetResponseStream()))
{
string result = streamReader.ReadToEnd();
}
如何避免超时?我应该在女服务员调用之间增加更多时间,更改女服务员线程数还是其他?