c# - Python 女服务员 API 挂起

标签 c# python api waitress

我正在使用 waitress 在本地主机(Windows VM)上提供 API,它由在同一 Windows VM 上运行的 C# 应用程序调用。当女服务员挂起时,C# 报告超时错误: System.Net.WebException:System.Net.HttpWebRequest.GetResponse() 操作已超时

我可以通过转到启动 waitress 的命令提示符并按 CTRL+C 来让 waitress 重新响应。当我这样做时,我从女服务员那里收到几条消息:警告:waitress.queue:任务队列深度为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# 代码 stub (如果重要的话)是:

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();
}

如何避免超时?我应该在女服务员调用之间添加更多时间、更改女服务员线程数还是其他什么?

最佳答案

当请求数多于线程数时,Waitress 会出现该错误 Source

WARNING:waitress.queue:Task queue depth is 6

显示您有 6 个请求在 1 个线程上等待。我建议增加线程数。

您可以增加请求之间的等待时间,但不能保证一次只有一个请求。

关于c# - Python 女服务员 API 挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55169231/

相关文章:

c# - 如何设置 Windows.Web.Http.HttpRequestMessage HttpVersion?

python - 如何修复此错误或对此错误进行异常(exception)处理

python - 将flask api封装在类中以供测试

python - 集成pylint和github评论评论

c# - 无法加载文件或程序集 System.Threading.Tasks,版本 = 2.5.19.0

c# - 是否可以使用容器在 .net 3.5 应用程序中加载 .net 4.0 应用程序?

python - 获取 os.walk 命中 abspath

python 嵌套打印出数据框中列的位置和名称

api - 如何在 golang 中使用 gin-gonic 服务器编写流 API?试过 c.Stream 没用

c# - WebResource.axd 和 ScriptResource.axd 加载非常慢