简体   繁体   中英

Python waitress API hangs

I'm using waitress to serve an API on localhost (Windows VM), it is being called by a C# application running on the same Windows VM. C# is reporting timeout errors when waitress hangs: System.Net.WebException: The operation has timed out at System.Net.HttpWebRequest.GetResponse()

I can kick waitress back into responding by going to the command prompt that launched waitress and pressing CTRL+C. When I do that, I get several messages from waitress: WARNING:waitress.queue:Task queue depth is 6 , but it proceeds to work.

Waitress was configured like this:

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)

and I run waitress with > python appname.py

The C# code stub, in case it matters, is:

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

How can I avoid the timeouts? Should I add more time between waitress calls, change the number of waitress threads, or something else?

Waitress gives that error when there are more requests than threads Source .

WARNING:waitress.queue:Task queue depth is 6

shows that you have 6 requests waiting on 1 thread. I would suggest increasing the number of threads.

You could increase the wait time between requests but there is no guarantee that there will only be one request at a time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM